Search code examples
iosuikitios6

iOS 6 UIDatePicker timer lets user select 0 countDownDuration


I'm using a UIDatePicker in UIDatePickerModeCountDownTimer to allow the user to select a duration.

If you set the timer interval to 5 minutes, the user will be able to select 0 hours : 0 minutes (which is bad; this is not allowed in iOS 5 or even in iOS 6 with a 1 minute interval).

Right now I'm fixing it by doing this on change:

-(void)timerValueChanged
{
    int clockInterval = workoutTimePicker.minuteInterval * 60;

    if (workoutTimePicker.countDownDuration < clockInterval) {
        workoutTimePicker.countDownDuration = clockInterval;
    }
}

But that makes the roller jump. How can I animate the roller?


Solution

  • There is currently no setCountDownDuration:animated: so we have to use setDate:animated:.

    We must create a NSDate object with current day/month/year to allow us to fill the minutes component, with your desired value, let's say it's workoutTimePicker.minuteInterval:

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
        components.hour = 0;
        components.minute = workoutTimePicker.minuteInterval; // how much minutes you want
        components.second = 0;
    NSDate * resetedMinsDate = [calendar dateFromComponents:components];        
    

    So with that new date, we just need to set the picker's date:

    [workoutTimePicker setDate:resetedMinsDate animated:YES];