1

If I understand correctly, the UITextFieldDelegate method textFieldShouldClear gets triggered once the user taps on the clear button/icon provided with and enabled for a UITextField. But I need to clear the UITextField programmatically, which, in the absence of a clear method for UITextField (that I could find), I am doing like so:

textField.text = @"";

The above, however, does not trigger the textFieldShouldClear delegate method that I need. Any ideas how I could do it?

Alternatively, does my call above trigger any other delegate method? I checked, and textInputChanged is not called in this case.

artooras
  • 6,315
  • 9
  • 45
  • 78

2 Answers2

1

TextField delegate methods will not fire if you set the text programmatically.

Related Question

You can probably just perform whatever logic needs to be done after your textField.text = @""; line though, right?

Community
  • 1
  • 1
Adam Kaump
  • 715
  • 4
  • 14
  • Thanks for pointing me to the relevant answer, it helped me figure out the solution that suited my situation - adding it as an answer – artooras Nov 25 '14 at 18:21
0

I ended up subclassing UITextField and implementing the following method:

- (void)setText:(NSString *)text {

    [super setText:text];

    [self textInputChanged:nil];
}

Where textInputChanged: is the UITextFieldDelegate method that I needed to get called whenever text is set programmatically.

artooras
  • 6,315
  • 9
  • 45
  • 78