如何检测 UITextField 何时更改并触发警报?

How can I detect when UITextField changed and trigger an alert?

我正在尝试检测用户何时更改输入字段。

我创建了这个函数

@objc func textFieldDidChange(_ textField: UITextField) {

    let alert = UIAlertController(title: "Did you bring your towel?", message: "It's recommended you bring your towel before continuing.", preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
    alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))

    self.present(alert, animated: true)

}

我这样添加了目标

passWord.addTarget(self, action: #selector(textFieldDidChange(passWord:)), for: .editingChanged)

我一直收到这个错误

Use of unresolved identifier 'textFieldDidChange(passWord:)'

如何防止此错误发生?

您的选择器有误...

passWord.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)

https://developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget

最简单的方法, 只需创建密码文本字段事件。