Tapping a UIScrollView to hide the keyboard?

2019-08-14 03:34发布

(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?

3条回答
甜甜的少女心
2楼-- · 2019-08-14 04:05

"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.

查看更多
家丑人穷心不美
3楼-- · 2019-08-14 04:06

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.

查看更多
戒情不戒烟
4楼-- · 2019-08-14 04:07

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

查看更多
登录 后发表回答