0

I have a UITextField in my ViewController, and the ViewController conforms UITextField delegate. Every thing is fine.

I set the UITextField clearable mode by:

[myTextField setClearButtonMode:UITextFieldViewModeWhileEditing];

The issue is, when I click the clear button of myTextField, the delegate method shouldChangeCharactersInRange cannot be triggered. Otherwise, if I type in characters or manually delete characters one by one, it will be triggered.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
}

For my understanding, clear the text is an text changed event, and I want to deal with this event in a common way, i.e. in the shouldChangeCharactersInRange. But it just not work. So, is there any way to connect to the clear text event to the delegate method? All discussion will be highly appreciated.

Vigor
  • 1,706
  • 3
  • 26
  • 47

1 Answers1

0
- (BOOL)textFieldShouldClear:(UITextField *)textField

this is the method called when the built-in clear button is pressed.

See the documentation for more help.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear:

:)

Leo
  • 24,596
  • 11
  • 71
  • 92