Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField Editing Changed not call

Tags:

ios

swift

I got a UITextField zipTextField and use UIPicker to change the text. And I want to know when the zipTextField is changed.

I put this program in my ViewController

@IBAction func textFieldEditingDidChange(sender: AnyObject){
    print("textField:\(zipTextField.text)")
}

And Connected to the View with Editing Changed Event connect to the view

But the function didn't call when zipTextField has changed

Otherwise when I connect textFieldEditingDidChange to Editing Did End or Editing Did Begin textFieldEditingDidChange will be called

like image 505
iamshaojin Avatar asked Apr 19 '18 10:04

iamshaojin


2 Answers

If you have implemented the delegate method

textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String)

and returned false, the event is not fired.

Usually this is because you're trying to manually set textfield's value (textField.text = newValue) preventing the user input.

If so, you have to add:

textField.sendActions(for: .editingChanged)

before returning false and after textField.text = newValue in the delegate method. This will send the UIControl Event back to your observer.

like image 73
Enrico F. Avatar answered Sep 28 '22 07:09

Enrico F.


If delegate function

textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String)

returns false, then function that set on .editingChanged will not call.

like image 29
Andrey M. Avatar answered Sep 28 '22 08:09

Andrey M.