views:

591

answers:

1

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?

+4  A: 
- (void) tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)_indexPath {
    if ([self.textView isFirstResponder])
        [self.textView resignFirstResponder];
    //...
}
Alex Reynolds
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
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
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