Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've been struggling with the maximum/minimum date lately, and honestly, I don't know what I'm missing here. I searched a lot and no one seems to complain about it. (I'm not talking about setting a maximum/minimum date as these can only make invalid dates appear gray)

Here's what I've done so far:

    [myDatePicker addTarget:self action:@selector(disableDate) forControlEvents:UIControlEventValueChanged];

Then, on disableDate, I have something like this:

if ([myDatePicker.date compare:[NSDate date]] == NSOrderedDescending) {
        MyDatePicker.date = [NSDate date];
    }

(Or ascending, it depends on which dates I want to disable)

The thing is: it works fine but only ONCE. In other words: as soon as I open the view and try to pick, for example, 2007, it scrolls back to 2013. Great. But right after that, if I try to scroll to any invalid date (including 2007), it won't scroll back anymore until I reopen my view. I feel like I'm missing something simple here, but I don't know what.

Thanks a lot in advance!

PS: Just to mention, I've put this inside disableDate

    NSLog(@"Value changed");

just to make sure it's calling the method everytime I scroll the datepicker, and yes, it is working;

PS2: I've tried the same but using

- (IBAction)datePickerValueChanged:(id) sender

instead and it had exactly the same behaviour.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
260 views
Welcome To Ask or Share your Answers For Others

1 Answer

So in order to "reset" the UIDatePicker you can use reloadInputViews method (since UIDatePicker is subclass of UIResponder

Edit: spelling


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...