如何将倒数计时器添加到 UIAlertController

How to add countdown-timer to UIAlertController

我是 Swift 的新手,对此找不到任何好的解释。我希望用户在警报中单击 UITextField 时 select 持续时间 (h:m:s) 并在 textField 中显示值。 有人可以给我提示或解释吗?

UITextField 具有名为 UITextFieldDelegate 的委托协议

在你的 viewController:

func viewDidLoad() {
    super.viewDidLoad()

    yourTextField.delegate = self
}

使您的 viewController 符合这位代表的要求。有一个 textFieldShouldBeginEditing 方法,当用户点击文本字段时调用它。你在这里 return false,所以 textField 不会成为第一响应者,键盘也不会打开,你可以显示你的选择器。

extension viewController: UITextFieldDelegate {

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        //open your alert here
        openPicker()

        return false
    }

}

您可以使用 UIPickerView 来呈现给用户。您可以在此处阅读相关内容,甚至可以根据示例创建它: explanation and example of UIPickerView