UITextField text change event

后端 未结 21 1429
南方客
南方客 2020-11-22 06:49

How can I detect any text changes in a textField? The delegate method shouldChangeCharactersInRange works for something, but it did not fulfill my need exactly.

21条回答
  •  我寻月下人不归
    2020-11-22 07:20

    Swift 4

    func addNotificationObservers() {
    
        NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChangeAction(_:)), name: .UITextFieldTextDidChange, object: nil)
    
    }
    
    @objc func textFieldDidChangeAction(_ notification: NSNotification) {
    
        let textField = notification.object as! UITextField
        print(textField.text!)
    
    }
    

提交回复
热议问题