Type 'NSNotification.Name' has no member 'UITextField'

邮差的信 提交于 2020-08-01 01:11:03

问题


With Swift 4.2, getting following error, that was working fine with Swift 4.

Type 'NSNotification.Name' has no member 'UITextField'

Here is my error code.

NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextField.textDidChangeNotification, object: textField, queue: OperationQueue.main) { (notification) in
            loginAction.isEnabled = textField.text != ""
        }

Full Code:

@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)
}


回答1:


textDidChangeNotification is a member of UITextField (and UITextView).

NotificationCenter.default.addObserver(
    self,
    selector: #selector(self.keyboardDidShow(notification:)),
    name: UITextField.textDidChangeNotification,
    object: nil)



回答2:


I faced the same problem,

Here is the simplest solution:

Instead of using forName: NSNotification.Name.UITextField.textDidChangeNotification

Use like this in forName: Parameter:

NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification, object: textField, queue: OperationQueue.main) { (notification) in
//Your code goes here...
        }



回答3:


        NotificationCenter.default.addObserver(forName: Notification.Name.UITextFieldTextDidChange, object: textField, queue: OperationQueue.main) { (notification) in
            //...
    }

In swift sdk, All notification name should be the extension of struct: Notification.Name

So when use Notification.Name, you should ignore Class name(exc.UITextField), input "Notification.Name." then input some of your name(like "TextF") and use esc to show autocomplete



来源:https://stackoverflow.com/questions/52324894/type-nsnotification-name-has-no-member-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!