1

Possible Duplicate:
Dismiss keyboard by touching background of UITableView

I have a custom UIScrollView which contains UITableView and UITextView. When user clicks on text field, keyboard pops up and it also disappears when user presses Return key on keyboard.

Question: How to get rid of keyboard when user taps on UITableView? No matter what I try, I just can't catch any touch events... Using iPhone 3.0 SDK since that does seem to make a big difference. As far as I understand touch events are not automatically redirected to... anywhere! Still there are touch events, since I can scroll table view up/down, even I don't want to!

What do I have to do to dismiss keyboard, when user presses somewhere else but keyboard itself or the text field?

Community
  • 1
  • 1
JOM
  • 8,139
  • 6
  • 78
  • 111
  • Check out the anser for [this](http://stackoverflow.com/questions/2321038) [question](http://stackoverflow.com/questions/2321038); it uses a very simple gesture recognizer to do what you need. – Zebs Feb 14 '11 at 17:27

1 Answers1

4
- (void) tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)_indexPath {
    if ([self.textView isFirstResponder])
        [self.textView resignFirstResponder];
    //...
}
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • It works! Don't know what to say, I'm overwhelmed... :D Ok, could you also please explain why does it work? Really curious! I tried earlier e.g. selectRowAtIndexPath, which was never called. What's this "tableView:(UITableView *)_tableView" magical thing and under what circumstances can it be used? – JOM Nov 26 '09 at 18:24
  • the "if([self.textView isFirstResponder]) is not necessary since resignFirstResponder will do nothing if it is not the first responder and will get rid of the keyboard if it is the first responder. – Devoted Jan 13 '10 at 07:17
  • That doesn't appear work if there is no row where they're tapping, ie. the TableView isn't full. How do you detect if they tap on the TableView itself? – zekel Jun 30 '10 at 19:15