51

I am currently trying to track the states of a textfield.

I am using a custom datePicker which sets the text via -setText

[self.textField setText:[self.dateFormatter stringFromDate:self.date]];

Inside my textFieldDelegate I wrote the following code:

-(void)viewDidLoad {
  [super viewDidLoad];
  self.travelToOnTextField.delegate = self;
  [self.travelToOnTextField addTarget:self action:@selector(travelToOnTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

-(void)travelToOnTextFieldDidChange:(id)sender
{
   NSLog(@"Event Handler called");
}

If I change the text through my custom Datepicker the Method travelToOnDateTextFieldDidChange doesn't get called. But if I change the text using my Computer Keyboard it gets called for some reason.

Is this intentional?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sebastian Boldt
  • 5,283
  • 9
  • 52
  • 64

5 Answers5

82

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shaolo
  • 1,180
  • 11
  • 13
  • 2
    Thanks for your solution, i also ended up using the sendActionsForControlEvents Method. – Sebastian Boldt Apr 09 '13 at 10:07
  • 1
    insertText doesn't work. But `[textField sendActionsForControlEvents:UIControlEventEditingChanged]` works. – Gon Mar 12 '16 at 07:55
  • `insertText(_:)` should work only if the text field is first responder because the documentation of `UIKeyInput` says it inserts text at the cursor position. – Raginmari May 03 '16 at 09:13
19

Just call sendActionsForControlEvents: after you set the text:

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

Tested on iOS 9.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Gon
  • 1,135
  • 12
  • 22
7

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 = nil
    taskTextField.sendActionsForControlEvents(UIControlEvents.EditingChanged)
    
zgorawski
  • 2,597
  • 4
  • 30
  • 43
5

Update: Swift 4.2

textField.text = "some Text"
textField.sendActions(for: UIControl.Event.editingChanged)
gayashanbc
  • 937
  • 1
  • 15
  • 30
4

The documentation says:

UIControlEventEditingChanged 
A touch making an editing change in a UITextField objet. 
Available in iOS 2.0 and later. 
Declared in UIControl.h.

So touch only?

Charlie Price
  • 1,266
  • 11
  • 20