【问题标题】:UIDatePicker's Value Changed Event Isn't Triggered When Value Is Changed For the First Time [duplicate]第一次更改值时不触发UIDatePicker的值更改事件[重复]
【发布时间】:2015-03-24 17:56:46
【问题描述】:

我使用 Swift 开发 iOS 应用程序。我在情节提要上有一个文本字段,需要将 UIDatePicker 设置为其输入视图。以下是我的代码。

@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。我还需要确保在第一次更改值时触发事件。我该如何解决这个问题?

谢谢。

【问题讨论】:

    标签: ios iphone ipad swift uidatepicker


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的回复。我设置默认值后,这个问题仍然存在。我刚刚更新了问题中设置默认值的代码。
    • 检查更新。这种方法可能对您更有效。我无法测试,因为我不在我的 Mac 附近,对此感到抱歉。
    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    相关资源
    最近更新 更多