Take the 2-minute tour ×

I have a UITableView that has some cells and I add a UITextField to each of the cells.

I set textField.clearButtonMode = UITextFieldViewModeWhileEditing.

When I am editing the textField, the clear button and the keyboard both come out. I type some words in the textField, then tap the clear button, the keyboard will be hidden but the text in the textField will not be cleared.

All of the others work well except the clear button.

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

I had this problem because I had forgotten I was using a UITapGestureRecognizer to catch taps on the table to dismiss the keyboard and it was capturing the tap on the clear button, preventing it from functioning. Add cancelsTouchesInView=NO on the UITapGestureRecognizer to let the touches still take effect and check using CGRectContainsPoint on the tapper method to only end editing and resignFirstResponder only when the tap wasn't on the current UITextField's frame rect. Note that this still isn't totally perfect since tapping X on an autocorrect might be outside the text field's frame rect, so maybe checking the cell's contentView would be better.

share|improve this answer
the same with you, I was also using a UITapGestureRecognizer to catch taps on the table. Thanks –  Hector Huang Aug 31 '12 at 10:09
Where should the cancelsTouchesInView=NO be performed? In the tap method? –  Johan Karlsson May 30 '13 at 12:01
A more complete example would be nice ;-) –  Johan Karlsson May 30 '13 at 12:20
@JohanKarlsson It's a property of the gesture recognizer. –  jszumski Jun 3 '13 at 18:54
add comment

I was unable to reproduce the issue you are having, since touching the Clear button does not and should not resign the first responder. But perhaps you can compare your code to the most basic use case I included below in order to find what went wrong.

Additionally, I would recommend reading the documentation about UIResponder since it seems like you may be accidentally dabbling in this area.

@implementation TextFieldTableViewController

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    // Return the number of sections.
    return 1;

    // Return the number of rows in the section.
    return 5;


    // Remove old instances of myTextField
    for (UIView *oldView in cell.contentView.subviews)
        [oldView removeFromSuperview];

    // Create my new text field
    UITextField *myTextField = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
    [myTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
    [myTextField setBorderStyle:UITextBorderStyleRoundedRect];

    // Add the TextField to the content view
    [cell.contentView addSubview:myTextField];

    return cell;

share|improve this answer
add comment

if you have got gesture Recogniser you should do like this way

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(methodThatYouMayCall)];
[myTextField addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate =  self;
gestureRecognizer.cancelsTouchesInView = NO;

and this will clear textfield as well as fires "methodThatYouMayCall" when you click on the clear button so that you should do this as well your textField.clearButtonMode is kind of UIButton class, so that you can do this way

   if ([touch.view isKindOfClass:[UIButton class]]) 
       return NO;
       return YES;

don't forget mark the class as implementing the UIGestureRecognizerDelegate protocol. hope this will help you.

share|improve this answer
add comment

Your Answer


