我想动态更改 UIDatePicker 的 datePickerMode
我希望能够有一个日期选择器可以输入月-日-年-小时-分钟。 我知道两个 UIDatePicker 对象是否是可能的。 例如 UIDatePicker obj1; //对于月-日-年 UIDatePicker obj2; //对于小时-分钟
但我不想有两个UIDatePicker用于一个日期输入:月-日-年-小时-分钟。 所以我做了一个UIDatePicker和一个UIButton。 该按钮用于更改 UIDatePicker 的 datePickerMode。
拾取器的形状可以改变。 [月|日|年] <--> [hh:mm]
但是当选择器的模式发生如下变化时,
[hh:mm] -> [月|日|年] -> [hh:mm]
选择器的小时日期变为 00:00,即使在模式更改之前设置为 11:59。
为什么?
无论模式如何改变,月日年都不会改变。
I want to be able to the one date picker can input month-day-year-hour-minute.
I know if two UIDatePicker objects were it's possible.
e.g.
UIDatePicker obj1; //for month-day-year
UIDatePicker obj2; //for hour-minute
But I don't want to have two UIDatePicker for one date input:month-day-year-hour-minute.
So I made one UIDatePicker and one UIButton.
The button is for change UIDatePicker's datePickerMode.
The picker's figure can be changed. [mm|dd|yyyy] <--> [hh:mm]
But when the picker's mode changes as following,
[hh:mm] -> [mm|dd|yyyy] -> [hh:mm]
the hour-date of the picker becomes 00:00, even if set like 11:59 before the mode changed.
Why?
The month-day-year never changes however the mode is changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是因为日期选择器存储日期的时间设置为午夜(如果处于 mm|dd|yyyy 模式)。因此,您必须将所选时间存储在单独的 NSDate 对象中,并且当用户从 mm|dd|yyyy 切换到 hh:mm 时,将日期选择器的日期设置为正确的日期和时间(通过 NSDateComponents 从一个日期中提取时间和分钟,从另一个日期中提取年/月/日,然后将所有组件组合成一个新日期)。
Probably because the date picker stores dates with the time set to midnight if it's in mm|dd|yyyy mode. So you would have to store the selected time in a separate
NSDate
object, and when the user switches from mm|dd|yyyy to hh:mm, set the date picker's date to the correct date and time (by extracting time and minute from the one date and year/month/day from the other viaNSDateComponents
and compositing a new date with all the components).谢谢您的回答。
我在原始代码中添加了 3 行,如下所示。
到目前为止似乎还不错。
Thank you for your answer.
I added 3 lines in my original codes as following.
It seems to be good so far.