How do I resignFirstResponder when I click outside of a UITextField on a UITableView

I'm having trouble getting the keyboard in my iPhone application to disappear, because even when I made the controller UIView insensitive, I have a UITableView that takes up the rest of the available screen.

I was curious to know how I would get out of the keyboard, aka firstResponder , by clicking on a UITableView ? Is there a way to track a touch event on a UITableView , even if it doesn't have to select an interactive cell.

Basically, I know how to change the keyboard if the cell fires an event, but if I click on the inaccurate part of the UITableView , I would still like the keyboard to go away.

+6
source share
4 answers

2 options:

  • In your viewController, answer the table scroll callback and cancel the responder
 -(void)scrollViewDidScroll:(UIScrollView *)scrollView { [self.view endEditing:YES]; } 
  • You can always add a UITapGestureRecognizer to the table / view and defer the responder from there

Personally, I usually do this on a scrolling table, since I don’t like a single keystroke to reject the keyboard.

+6
source
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)]; doubleTap.numberOfTapsRequired = 1; [self.tableView addGestureRecognizer:doubleTap]; [doubleTap release]; } - (IBAction)tapDetected:(UIGestureRecognizer *)sender { CGPoint p = [sender locationInView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p]; if(indexPath == nil) { NSLog(@"empty"); } else { [textField resignFirstResponder]; } } 

I think this will help ... try ...

+3
source

Adding a gesture recognition flag is an interesting solution, but there is an alternative, and you do not need to encode anything!

You can set the keyboardDismissMode property in the Interface Builder to "Reject Drag and Drop" for your table view. This is a property inherited from UIScrollView, and whenever you view a view in a table, the keyboard is rejected.

+3
source
 @property (weak, nonatomic) UITextField *activeTextField; // keeps track of whose keyboard is being displayed. - (void)textFieldDidBeginEditing:(UITextField *)textField { self.activeTextField = textField; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // if (indexPath equal to something then) [self.activeTextField resignFirstResponder]; } 
0
source

Source: https://habr.com/ru/post/909649/


All Articles