UIControlEventEditingChanged doesn't get fired when using setText of UITextfield UIControlEventEditingChanged doesn't get fired when using setText of UITextfield ios ios

UIControlEventEditingChanged doesn't get fired when using setText of UITextfield


I had to rate this question up because this is super important.

It appears this has changed in iOS6. Maybe even iOS5. This event USED to be the preferred method for observing text changes in a UITextField. I've been using it since iOS3. After recompiling my app on iOS6 to perform some updates, it mysteriously stopped working. Parts of my app use:

[UITextField insertText];

and others use

[UITextField setText];

or

UITextField.text = @"xxx";

For whatever reason it's important to note that setText events no longer fire the Editing Changed event. If you use an insertText method, it still works.

So if you're in a bind and you just need a quick fix, you can change out your code from:

textField.text;

or

[textField setText:@"xxx"];

to

textField.text = @"";[textField insertText:@"new text"];

This also works:

[((UITextField*)view) sendActionsForControlEvents:UIControlEventEditingChanged];

I hope this helps someone, because I wasted an entire day trying to figure out why this code suddenly stopped working when I moved up to iOS6.

You can still use NSNotificationCenter as well. See the UITextField reference for more info:

https://developer.apple.com/documentation/uikit/uitextfield


Just call sendActionsForControlEvents: after you set the text:

self.textField.text = xxx;[self.textField sendActionsForControlEvents:UIControlEventEditingChanged];

Tested on iOS 9.


In Swift 2.2+, based on @Shaolo's answer

  1. Connect IBAction, pay attention to change default event type to EditingChanged

enter image description here

  1. After you change textField's text from code, add also sendActionsLine

    taskTextField.text = niltaskTextField.sendActionsForControlEvents(UIControlEvents.EditingChanged)