在 Swift 4.2 中,出现以下错误,在 Swift 4 中工作正常。
类型“NSNotification.Name”没有成员“UITextField”
这是我的错误代码。
NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextField.textDidChangeNotification, object: textField, queue: OperationQueue.main) { (notification) in
loginAction.isEnabled = textField.text != ""
}
完整代码:
@IBAction func alertWithLogin(){
let alertController = UIAlertController(title: "Please Enter Credential", message: nil, preferredStyle: .alert)
// ADD ACTIONS HANDLER
let loginAction = UIAlertAction(title: "Login", style: .default) { (_) in
let loginTextField = alertController.textFields![0] as UITextField
let passwordTextField = alertController.textFields![1] as UITextField
// do something with after login
}
loginAction.isEnabled = false
alertController.addAction(loginAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in
// do something
}
alertController.addAction(cancelAction)
// ADD TEXT FIELDS
alertController.addTextField { (textField) in
textField.placeholder = "Email"
}
alertController.addTextField { (textField) in
textField.placeholder = "Password"
textField.isSecureTextEntry = true
// enable login button when password is entered
NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextField.textDidChangeNotification, object: textField, queue: OperationQueue.main) { (notification) in
loginAction.isEnabled = textField.text != ""
}
}
// PRESENT
present(alertController, animated: true)
}
textDidChangeNotification
是的成员UITextField
(and UITextView
).
NotificationCenter.default.addObserver(
self,
selector: #selector(self.keyboardDidShow(notification:)),
name: UITextField.textDidChangeNotification,
object: nil)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)