如何将UIDatePicker更改为特定时间(以代码形式) [英] How to change UIDatePicker to a specific time (in code)

查看:119
本文介绍了如何将UIDatePicker更改为特定时间(以代码形式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态地将UIDatePicker更改为特定的

I need to change the UIDatePicker to a specific dynamically.

我的日期选择器仅设置为时间模式. 我可以用

My date picker is set to time mode only. i can set the time with

timePicker.setDate(NSDate(), animated: false)

但是我不知道如何将其更改为其他时间,而不是 当前时间.

but i cant figure out how to change it to a different time and not to current time.

那我该如何更改呢?

谢谢

推荐答案

您必须更改时间,可以使用NSDateComponents进行设置,并将修改后的日期设置为DatePicker

You've to change the time, you can do it using NSDateComponents and set the modified date to your DatePicker

var calendar:NSCalendar = NSCalendar.currentCalendar()
let components = calendar.components(NSCalendarUnit.HourCalendarUnit | NSCalendarUnit.MinuteCalendarUnit, fromDate: NSDate())
components.hour = 5
components.minute = 50
datePicker.setDate(calendar.dateFromComponents(components)!, animated: true)

这篇关于如何将UIDatePicker更改为特定时间(以代码形式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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