views:

113

answers:

3

(I posted this before anonymously, but then couldn't use the same computer again, so I'm posting it from my account now. Sorry to the guy who answered before.)

I'm working on an iPhone app which involves typing stuff into a UITextView, which adds content to a UITableView. The problem is, I need to be able to close the keyboard when the user's done with it, and the only area that is really visible other than the keyboard and UITextView at this point is the UITableView. I'm having trouble implementing a touch event on the UITableView (as in, touching the UITableView anywhere, not just didSelectRowAtIndexPath:). Here's the code I'm using in the view controller, but for some reason, it's not being executed at all:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 [textView resignFirstResponder];
} 

Any suggestions?

+1  A: 

"Here's the code I'm using in the view controller"

That's your problem - you have to create a subclass of UITableView and put the function in there before you will get those touch events.

Adam Eberbach
Thanks for that, I'm getting the touch events now - but the keyboard still isn't hiding. Any idea on how to get that keyboard to hide?
Debashis
try checking [textView isFirstResponder] - chances are you are sending resignFirstResponder to the wrong object if it is not going away.
Adam Eberbach
Cool, I just looped through all of the UITableView's superview's subviews, checked for isFirstResponder, and called resignFirstResponder if it returned true. Thanks a lot for your help, you have no idea how much I appreciate it.
Debashis
A: 

Put an invisible button over the UITableView and trap the taps.

BojanG
But wouldn't the user not be able to scroll the UITableView if I did that?
Debashis
A: 

Yeah, I just said put a breakpoint in to make sure resignFirstResponder is actually being called and that it is being called on the correct text field.

Matt Williamson