Search code examples
iphoneuitableviewkeyboarduitextfieldfirst-responder

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


I am having trouble getting the keyboard in my iPhone app to go away because the UIView even when made a controller is not touchable because of the fact that I have a UITableView taking up the rest of the available screen.

I was curious to know how I would go resigning the keyboard aka firstResponder by clicking onto the UITableView? Is there a way to monitor a touch event on the UITableView even if it is not to select a clickable cell.

Basically, I know how to resign the keyboard if the cell fires the event but, if I click on a non - clickable part of the UITableView I would still like the keyboard to go away.


Solution

  • 2 options:

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

    Personally I usually do it on table scroll, since I don't like a single tap to dismiss the keyboard.