Question

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.

Était-ce utile?

La solution

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

Edit: spelling

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top