ios - UIDatePicker的Value Changed事件在第一次改变值时不触发

标签 ios iphone ipad swift uidatepicker

<分区>

我使用 Swift 开发一个 iOS 应用。我在 Storyboard上有一个文本字段,需要将 UIDatePicker 设置为其输入 View 。以下是我的代码。

@IBOutlet var textField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    var datePicker: UIDatePicker = UIDatePicker()
    datePicker.datePickerMode = UIDatePickerMode.CountDownTimer
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "HH:mm"
    datePicker.setDate(dateFormatter.dateFromString("01:00")!, animated: false)
    datePicker.addTarget(self, action: "datePickerValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    textField.inputView = datePicker
}

func datePickerValueChanged(sender: UIDatePicker) {
    println("Event is triggered.")
}

当我第一次运行应用程序并更改值时,未触发 ValueChanged 事件。当我第二次更改值时,事件被触发。

如果我注释代码datePicker.datePickerMode = UIDatePickerMode.CountDownTimer,该事件将在第一次更改值时触发。

我需要将 UIDatePickerMode 自定义为 CountDownTimer。我还需要确保在第一次更改值时触发事件。我该如何解决这个问题?

谢谢。

最佳答案

这可能是 CountDownTimer 模式下 UIDatePicker 的错误(它可能没有设置默认值来识别“更改”,但不是 100% 确定)。

要解决此问题,您可以在 viewDidLoad 方法中执行以下操作:

 dispatch_async(dispatch_get_main_queue(), ^{
      datePicker.countDownDuration = (NSTimeInterval) duration;
 });

关于ios - UIDatePicker的Value Changed事件在第一次改变值时不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29239681/

相关文章:

ios - 取消订阅 Apple 推送通知服务

iphone - 将 NSArray 项添加到 UITableViewCell 内容 View

ipad - 如何在多台 iPad 上安装 iPad 应用程序?

ios - Kiosk 模式 iOS 应用程序和从应用程序商店自动更新

iphone - 如何将数据导入/导出 iPhone 应用程序?

iphone - 编辑更改在 IOS5 中有效,但在 IOS6 中无效。文本字段 : shouldChangeCharactersInRange:

iphone - UITableViewCell ClipsToBound 不起作用

iphone - 如何在 iOS 上的 mono 中重写 ClientBase.CreateChannel

ios - 我应该在 iOS 应用程序的 git 存储库中忽略哪些文件/文件夹?

iphone - 如何从AudioQueueBufferRef->mAudioData读取数据