Reputation: 2921
I'd like to execute code after the clear button of an UITextField has been tapped and after the text field has been cleared.
textFieldShouldReturn: is called before the text field is cleared.
textField:shouldChangeCharactersInRange:replacementString: is not called at all when the clear button is tapped
The ultimate goal is that I am maintaining some graphical representation of the string being tapped in when it is tapped. I used textField:shouldChangeCharactersInRange:replacementString: for that, and it works well, except when the clear button is tapped. (which should erase the graphical representation).
Upvotes: 2
Views: 393
Reputation: 9143
Here's an idea: you can replace the system clear button with your own button. You'll need to handle clearing the text-field yourself (very simple) and then you can perform your custom animations and what not.
Create a button and set it as the rightView
of your UITextField
:
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeSystem];
clearButton.frame = CGRectMake(0, 0, 45, 45);
clearButton.tintColor = myTextField.tintColor;
[clearButton setImage:[UIImage imageNamed:@"MY_CLEAR_IMAGE.png"] forState:UIControlStateNormal];
[clearButton addTarget:self action:@selector(onTextfieldClearButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
myTextField.rightView = clearButton;
myTextField.rightViewMode = UITextFieldViewModeWhileEditing;
Handle the action:
-(void)onTextfieldClearButtonTouchUpInside:(UIButton*)clearButton
{
if([clearButton.superview isKindOfClass:[UITextField class]])
{
((UITextField*)(clearButton.superview)).text = @"";
//TODO: YOUR MAGIC GOES HERE
}
}
Upvotes: 2