I want to ask that how can I set the iOS Count Down Timer's maximum time? (eg. 1 hour and 30 minutes at most)
Count Down Timer is get from UIDatePicker
's mode:
Thanks!
EDIT:
Someone said I have to set the minimum/maximum date, and I just set them in the storyboard but I don't see any difference:
(the time of the setting is my local time +- 30 minutes)
EDIT:
From Apple:
The minimum and maximum dates are also ignored in the countdown-timer mode (UIDatePickerModeCountDownTimer).
So is their anyway to do this?
While Niko's solution works, it's downside is that the count of hours remains large. If you want to actually limit the hours that are displayed, I built a custom picker for just that purpose. It's a subclass of UIPickerView
and it replicates the functionality of UIDatePicker in countDownTimer mode, while adding support to set maxTimeInterval
.
You use it like this:
GSTimeIntervalPicker *picker = [[GSTimeIntervalPicker alloc] init];
picker.maxTimeInterval = (3600 * 3); // set the limit
picker.minuteInterval = 5; // the step. Default is 1 min.
picker.timeInterval = (3600 * 1.5); // 1 h 30 minutes
picker.onTimeIntervalChanged = ^(NSTimeInterval newTimeInterval) {
// Use the value
};
Available on GitHub under MIT license. Blog post here.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With