如何设置 UIDatePickerModeCountDownTimer 的最长时间? [英] How can I set the UIDatePickerModeCountDownTimer's maximum time?

查看:28
本文介绍了如何设置 UIDatePickerModeCountDownTimer 的最长时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下,如何设置iOS Count Down Timer的最长时间?(例如,最多 1 小时 30 分钟)

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)

倒数计时器是从 UIDatePicker 的模式中获取的:

Count Down Timer is get from UIDatePicker's mode:

谢谢!

有人说我必须设置最小/最大日期,我只是在故事板中设置了它们,但我看不出任何区别:

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:

(设置的时间是我当地时间+- 30分钟)

(the time of the setting is my local time +- 30 minutes)

来自苹果:

在倒数计时器模式 (UIDatePickerModeCountDownTimer) 中,最小和最大日期也会被忽略.

The minimum and maximum dates are also ignored in the countdown-timer mode (UIDatePickerModeCountDownTimer).

他们到底要不要这样做?

So is their anyway to do this?

推荐答案

对于 UIDatePickerModeCountDownTimer,您可以以编程方式处理它

In your case for UIDatePickerModeCountDownTimer you can handle it programmatically

添加一个在 UIDatePicker 的值发生变化时调用的事件.

Add an event called when the value of your UIDatePicker has changed.

目标 C

[self.datePicker addTarget:self action:@selector(datePickedValueChanged:)
     forControlEvents:UIControlEventValueChanged];

迅捷

self.datePicker.addTarget(self, action: Selector("datePickedValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)

然后如果选择的值超出了允许的选择范围,您可以将 DatePicker 设置为允许的最大值(或任何您想要的值)

Then if the value selected is out of allowed selection, you can set the DatePicker to maximum allowed valued (or whichever you want)

目标 C

- (void)datePickedValueChanged:(id)sender{
    if (self.datePicker.countDownDuration > 5400.0f) { //5400 seconds = 1h30min
        [self.datePicker setCountDownDuration: 60.0f]; //Defaults to 1 minute
    }
}

迅捷

func datePickedValueChanged (sender: UIDatePicker) {
    if (self.datePicker.countDownDuration > 5400) { //5400 seconds = 1h30min
        self.datePicker.countDownDuration = 60.0; //Defaults to 1 minute
    }
}

-- 上一个答案:

我在 Date 或 DateAndTime 模式下使用 UIDatePicker 为其他人留下以前的答案,如果这可以帮助某些人

I leave previous answer for others using a UIDatePicker in Date or DateAndTime mode, if that can help some people

您可以设置UIDatePicker的最小和最大日期.

You can set minimum and maximum date of your UIDatePicker.

这里用户不能选择当前时间之前的时间,只能提前 1 小时 30 分钟.

Here user can't select a time before present time, and just go ahead 1 hour and 30 minutes.

任何选择其他时间的尝试都会使 UIDatePicker 自动返回到允许的时间间隔.

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];

这篇关于如何设置 UIDatePickerModeCountDownTimer 的最长时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆