Reputation: 475
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.
Upvotes: 5
Views: 3801
Reputation: 6145
While the original issue was not caused by this, I think any future searches that lead here may need to know:
Views you add to a UITableViewCell
must be added as subviews to its contentView
property, otherwise you may experience the views being displayed with no user interactions enabled.
You have been supposed to add subviews to contentView
since iOS 2.0.
Upvotes: 0
Reputation: 277
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
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]])
{
return NO;
}
else
{
return YES;
}
}
don't forget mark the class as implementing the UIGestureRecognizerDelegate protocol. hope this will help you.
Upvotes: 3
Reputation: 16906
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.
Upvotes: 10
Reputation: 743
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;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
// 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;
}
@end
Upvotes: 1