UIDatePicker设置为15分钟间隔,但是如果用户不滚动,则日期间隔不是15分钟 [英] UIDatePicker is set to 15 minute intervals, but the date if the user doesn't scroll isn't in 15 minute intervals

查看:321
本文介绍了UIDatePicker设置为15分钟间隔,但是如果用户不滚动,则日期间隔不是15分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用两个UIDatePickers的iOS应用程序。它们仅显示小时和分钟(尽管我的应用仍然使用月,日和年)。在main.storyboard中,我已将UIDatePickers的Date值设置为Current Date。而且,我将它们的间隔设置为15分钟。

I am creating an iOS application that uses two UIDatePickers. They show just hours and minutes (my app still uses the month, day, and year though). In main.storyboard I have set the UIDatePickers' Date values to Current Date. And, I have set their interval to 15 minutes.

问题是,如果我不滚动UIDatePickers,则从它们中获得的日期值将不在间隔15分钟。如果我滚动它们,我确实会得到15分钟的间隔。

The issue is that if I don't scroll the UIDatePickers, the date value I get from them isn't in 15 minute intervals. If I scroll them I do get 15 minute intervals.

示例:
实际时间是8:47 PM。 UIDatePicker加载到8:45 PM。我不滚动而从中获得的日期值为8:47 PM,而不是8:45 PM。如果我滚动到晚上8:30或9:00 PM,我会得到那些时间。

Example: The actual time is 8:47PM. The UIDatePicker loads to 8:45PM. The date value I get from it without scrolling will be 8:47PM, not 8:45PM. If I scroll up to 8:30PM or 9:00PM, I will get those times.

我希望每15分钟间隔一次。有没有办法做到这一点,还是我必须编写一个函数以四舍五入到最接近的15分钟间隔?

I would like to get my time in 15 minute intervals. Is there a way to do this, or do I have to write a function to round to the nearest 15 minute interval?

推荐答案

实际上,事实证明有人在Swift 3中为此编写了扩展名。我的错。此解决方案对我来说是完美的。

Actually, it turns out someone has written an extension for this in Swift 3. My mistake. This solution worked perfectly for me.

https://stackoverflow.com/ a / 42263214/7025448

这篇关于UIDatePicker设置为15分钟间隔,但是如果用户不滚动,则日期间隔不是15分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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