Tapping a UIScrollView to hide the keyboard?

江枫思渺然 提交于 2019-12-24 10:48:16

问题


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


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/3499810/tapping-a-uiscrollview-to-hide-the-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!