Kru*_*nal 12 nsnotificationcenter ios swift4.2
使用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 != ""
}
Run Code Online (Sandbox Code Playgroud)
完整代码:
@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)
}
Run Code Online (Sandbox Code Playgroud)
rma*_*ddy 34
textDidChangeNotification是UITextField(和UITextView)的成员.
NotificationCenter.default.addObserver(
self,
selector: #selector(self.keyboardDidShow(notification:)),
name: UITextField.textDidChangeNotification,
object: nil)
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,
这是最简单的解决方案:
而不是使用 forName: NSNotification.Name.UITextField.textDidChangeNotification
在forName:参数中像这样使用:
NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification, object: textField, queue: OperationQueue.main) { (notification) in
//Your code goes here...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8327 次 |
| 最近记录: |