swift - 在倒计时器模式下在 UIDatePicker 上设置日期

标签 swift uidatepicker

我在倒计时器模式下使用UIDatePicker供用户输入行程时间: enter image description here

我可以像这样返回以秒为单位的时间:

 @IBAction func customTimeSelected(_ sender: Any) {
     print("user selected custom time: \(customeTimePicker.countDownDuration)")
}

但是如何以秒为单位设置UIDatePicker

这是我的尝试:

let travelDate = Date(timeIntervalSinceNow: TimeInterval(5400))  // hour and a half in seconds
self.customeTimePicker.setDate(travelDate, animated: false)

但这会返回不正确的时间:

enter image description here

最佳答案

刚刚发现正确的方法是在计时器上使用 countDownDuration 而不是 setDate。也不需要日期,只需一个TimeInterval:

self.customeTimePicker.countDownDuration = TimeInterval(5400)

关于swift - 在倒计时器模式下在 UIDatePicker 上设置日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47612997/

相关文章:

ios - UILabel 按下时会使应用程序崩溃

ios - 在右侧而不是左侧显示 UIToolbar 的 UIBarButtonItem

ios - 在 iOS 中保护数据。钥匙串(keychain)或任何其他方法?

iphone - 保存和加载数据 - CoreData

objective-c - UIDatePicker 在配置为使用 30 分钟间隔时设置不正确的时间

swift - 在 iOS 14 中以编程方式打开 UIDatePicker

ios - 如何将日期传回父类并更改标签?

swift - 无法使用 'Self' 类型的索引为 'Int' 类型的值添加下标

swift - 下拉菜单标题与条形按钮重叠 - Swift

ios - 如何在点击按钮时撤消表格 View 单元格的重新排序