UITextField text change event

后端 未结 21 1384
南方客
南方客 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:46

    Swift 4 Version

    Using Key-Value Observing Notify objects about changes to the properties of other objects.

    var textFieldObserver: NSKeyValueObservation?
    
    textFieldObserver = yourTextField.observe(\.text, options: [.new, .old]) { [weak self] (object, changeValue) in
      guard let strongSelf = self else { return }
      print(changeValue)
    }
    

提交回复
热议问题