In your case for UIDatePickerModeCountDownTimer you can handle it programmatically
Add an event called when the value of your UIDatePicker
has changed.
Objective-C
[self.datePicker addTarget:self action:@selector(datePickedValueChanged:)
forControlEvents:UIControlEventValueChanged];
Swift
self.datePicker.addTarget(self, action: Selector("datePickedValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)
Then if the value selected is out of allowed selection, you can set the DatePicker to maximum allowed valued (or whichever you want)
Objective-C
- (void)datePickedValueChanged:(id)sender{
if (self.datePicker.countDownDuration > 5400.0f) { //5400 seconds = 1h30min
[self.datePicker setCountDownDuration: 60.0f]; //Defaults to 1 minute
}
}
Swift
func datePickedValueChanged (sender: UIDatePicker) {
if (self.datePicker.countDownDuration > 5400) { //5400 seconds = 1h30min
self.datePicker.countDownDuration = 60.0; //Defaults to 1 minute
}
}
-- Previous answer :
I leave previous answer for others using a UIDatePicker
in Date or DateAndTime mode, if that can help some people
You can set minimum and maximum date of your UIDatePicker
.
Here user can't select a time before present time, and just go ahead 1 hour and 30 minutes.
Any attempt to select another time will make the UIDatePicker
to automatically go back to an allowed time interval.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *dateDelta = [[NSDateComponents alloc] init];
[dateDelta setDay:0];
[dateDelta setHour:1];
[dateDelta setMinute:30];
NSDate *maximumDate = [calendar dateByAddingComponents:dateDelta toDate:currentDate options:0];
[self.datePicker setMaximumDate:maximumDate];
[dateDelta setDay:0];
[dateDelta setHour:0];
[dateDelta setMinute:0];
NSDate *minimumDate = [calendar dateByAddingComponents:dateDelta toDate:currentDate options:0];
[self.datePicker setMinimumDate:minimumDate];