I can add a small cross button that is used to clear all the text in a single click in a UITextField
with the following code.
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
I also implemented UITextFieldDelegate
.
I want to activate/deactivate the right bar button of the UINavigationBar
according to the changes in my UITextField
. The rule is simple: if the text field has at least one character, just enable the button, else disable it.
I implemented the UITextField
's delegate method textField:shouldChangeCharactersInRange:
to constantly check the requirement and update the status of the button.
Now my problem is, when I click the small cross button on the text field, the method textField:shouldChangeCharactersInRange:
is not called. So the required action is missing. How can I identify the hit event of the clear button?
Some pictures to show what I'm talking about.
Initial stage: here, the "Done" button is disabled because there are no letters in the text field
When typing a letter, the "Done" button is enabled
Now, if I click on the clear button, the "Done" button is still enabled. This is wrong.
Use the delegate method
textViewShouldClear:
:Declared In UITextField.h
You can use
textFieldShouldClear:
delegate method to handle when user taps on clear button.