UIDatePicker 间隔 15m,但始终作为返回值的精确时间
我有一个时间模式下的 UIDatePicker,间隔为 15 分钟。
假设现在是晚上 7:22。日期选择器默认显示的值是四舍五入到下一个较高/较低值的当前时间,在本例中为晚上 7:15。如果用户与日期选择器交互,则显示的值也是返回的值。但是,如果没有任何用户交互,并且我使用 [UIDatePicker date]
获取时间,则返回值是准确的当前时间,即 7:22(继续使用示例)。
即使用户没有明确选择一个值,是否有任何方法可以始终获取日期选择器上实际显示的值而不是当前的未舍入时间?
我已经尝试使用 [UIDatePicker setDate:[NSDate date]]
手动设置选择器,但这不会改变行为。
I've got a UIDatePicker in Time mode with an interval of 15 minutes.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@ima747 是正确的,如果我更改了日期选择器的默认值,那么它工作正常,如果我在没有 onChange 的情况下从选择器控件中读取了日期,那么它会返回错误的日期值。
但我已经检查过,日期选择器控件已设置默认日期(当前日期,可能使用最近的时间毫秒)如果我将其设置为自定义日期并将时间设置为任何日期和 1:00:AM,那么现在我的时间选择器总是以 1:00:AM 而不是当前时间打开
@ima747 is right if I've changed date picker's default value then it's working fine and if I've read date from picker control without onChange then it's returning wrong date value.
But I've checked that, date picker control have set default date (current date and may be it use nearest time milliseconds) If I set it to custom date and also set time to any date and 1:00:AM so now my time picker always open with 1:00:AM instead of current time
我已经修改了 @ima747 的 Swift 3/4 答案并作为
UIDatePicker
的扩展。picker.clampedDate
I've modified @ima747 's answer for Swift 3/4 and as an extension of
UIDatePicker
.picker.clampedDate
这不是一个完美的解决方案,但它对我有用。我从另一篇文章中获取了它并对其进行了修改以接受不同的值。
向其提供输入日期和分钟值(在此线程的情况下取自 UIDatePicker.monthInterval),它将返回一个固定在该间隔的时间,您可以将其提供给选择器。
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.
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.