UIDatePicker,间隔为15米,但始终为返回值 [英] UIDatePicker with 15m interval but always exact time as return value

查看:107
本文介绍了UIDatePicker,间隔为15米,但始终为返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在时间模式下有一个UIDatePicker,间隔为15分钟。

I've got a UIDatePicker in Time mode with an interval of 15 minutes.

让我们说是晚上7:22。日期选择器默认显示的值是四舍五入到下一个更高/更低值的当前时间,在这种情况下,它是下午7:15。如果用户与日期选择器交互,则显示的值也是返回的值。但是,如果没有任何用户交互,我得到 [UIDatePicker日期] 的时间,返回值是当前的确切时间,即7:22坚持例。有没有办法总是获得实际显示在日期选择器上的值而不是当前的未连接时间,即使用户没有明确选择值?我已经尝试使用 [UIDatePicker setDate:[NSDate date]] 手动设置选择器,但这不会改变行为。

Let's say it's 7:22PM. The value that the date picker shows per default is the current time rounded to the next higher/lower value, in this case, it's 7:15PM. If the user interacts with the date picker, the value that's shown is also the value returned. However if there hasn't been any user interaction and I get the time with [UIDatePicker date] the return value is the exact current time, i.e. 7:22 to stick with the example. Is there any way to always get the value that's actually shown on the date picker instead of the current unrounded time, even if the user hasn't explicitly picked a value? I've already tried to set the picker manually with [UIDatePicker setDate:[NSDate date]] but that doesn't change the behavior.

任何提示?在此先感谢!

Any hints? Thanks in advance!

推荐答案

这不是一个完美的解决方案,但它适用于我。我从另一篇文章中获取并修改它以接受不同的值。

This isn't a perfect solution but it works for me. I've taken it from another post and modified it to accept different values.

- (NSDate*)clampDate:(NSDate *)dt toMinutes:(int)minutes {
int referenceTimeInterval = (int)[dt timeIntervalSinceReferenceDate];
int remainingSeconds = referenceTimeInterval % (minutes*60);
int timeRoundedTo5Minutes = referenceTimeInterval - remainingSeconds; 
if(remainingSeconds>((minutes*60)/2)) {/// round up
    timeRoundedTo5Minutes = referenceTimeInterval +((minutes*60)-remainingSeconds);            
}
 return [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)timeRoundedTo5Minutes];
}

输入输入日期和分钟值(取自UIDatePicker.minuteInterval)在这个线程的情况下)它将返回一个时间钳位到该间隔,你可以将其提供给选择器。

Feed it an input date and a minute value (taken from the UIDatePicker.minuteInterval in the case of this thread) and it will return a time clamped to that interval which you can feed to the picker.

这篇关于UIDatePicker,间隔为15米,但始终为返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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