简体   繁体   English

UIDatePicker 模式 countDownTimer 值更改回调未第一次调用

[英]UIDatePicker mode countDownTimer value changed callback not calling first time

I am having this strange issue in UIDatePicker mode.countdownTimer我在 UIDatePicker mode.countdownTimer 中遇到了这个奇怪的问题

在此处输入图像描述

When I add constraints of up/down/left/right to the datePicker view, the callback is not registered for the first time.当我在 datePicker 视图中添加上/下/左/右约束时,第一次没有注册回调。 However, if I add only Alignment constraints, the callback works everytime.但是,如果我只添加 Alignment 约束,则回调每次都有效。 Maybe its a bug.也许它是一个错误。 Any idea why isn't it working?知道为什么它不起作用吗?

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var datePicker: UIDatePicker!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        datePicker.datePickerMode = .countDownTimer
        datePicker.addTarget(self, action: #selector(datePickerValueChanged(_:)), for: .valueChanged)

    }

    @objc func datePickerValueChanged(_ sender: UIDatePicker){
        print("Selected value \(sender.countDownDuration)")
    }
}

OK, it's a long-known bug in iOS, since iOS 7 or so.好的,这是 iOS 中的一个长期已知的错误,因为 iOS 7 左右。 You can work-around in your case if you put the following somewhere into viewDidLoad :如果将以下内容放入viewDidLoad中,则可以解决您的情况:

DispatchQueue.main.async {
    self.datePicker.countDownDuration = self.datePicker.countDownDuration
}

Seems nasty, and is nasty.看起来很恶心,也很恶心。

The workaround of setting the countDownDuration didn't work for me.设置 countDownDuration 的解决方法对我不起作用。 Instead the only way I managed to solve this is to replace the UIDatePicker with a UIPickerView and populate it myself with 4 components: hours, "hour(s)" label, mins, "min(s)" label.相反,我设法解决这个问题的唯一方法是用 UIPickerView 替换 UIDatePicker 并用 4 个组件自己填充它:小时、“小时”label、分钟、“分钟”label。

  • The hours are 0-23时间是 0-23
  • The hours label is a single row component, which gets reloaded when the hours are changed so that it switches to "hour" when it's 1 hour小时 label 是一个单行组件,当小时更改时会重新加载,以便在 1 小时时切换到“小时”
  • The mins are 0-59分钟是 0-59
  • The mins label is the same as the hours label分钟 label 与小时 label 相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM