通过触摸具有嵌套视图层次结构的 UITextView 之外的某个地方来 resignFirstReponder
我关于 stackoverflow 的第一个问题...我很紧张;)
好吧,当我触摸视图层次结构的其余部分的某些位置时,我很难让我的 UITextView resignFirstResponder 。 是这样的:
UIView -> UITableView-> UITableCell-> UIView(我的容器视图)-> UIView (我的 bubbleview) -> UITextView
由于UITextView是FirstResponder,我们可以填充一些文本,并显示键盘。如果我们不必通过按下按钮来关闭键盘,而只需点击表面上 UITextView 外部的某个位置,那就太好了。
但是我应该如何以及在哪里响应点击手势?在链中某处的控制器上。我没有到处引用 UITextView 来向它发送消息(resignFirstReponder)...
我希望我能说清楚。我怎样才能通过触摸表面的某个地方来摆脱键盘......
希望有人能给一些提示......
问候,
Jeroen
my first question on stackoverflow...i'm very nervous ;)
Ok, i have troubles to make my UITextView resignFirstResponder on the moment i touch some where on the rest of my view hierarchy.
It's like this:
UIView -> UITableView -> UITableCell -> UIView (my containerview) -> UIView (my bubbleview) -> UITextView
As the UITextView isFirstResponder, we can fill in some text, and keyboard is shown. It would be nice if we do not have to dismiss the keyboard by pressing a button, but just by tapping outside the UITextView somewhere on the surface.
But how and where should i respond to a tap gesture? And on that controller somewhere in the chain. I don't have everywhere a reference to the UITextView to send it a message (resignFirstReponder) to...
I hope i can make myself clear. How can i get rid of of the keyboard by just touch somewhere on the surface....
Hopefully somebody can give some hints...
Regards,
Jeroen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您触摸文本字段边界之外时,您可以使用 NSNotificationCenter 来触发通知,文本字段将通过 resignFirstResponder 进行响应。
编辑更多细节:
您可以在 UIViewController/View 上实现以下方法,
这是一种比较便宜的方法,但确实有效。
You could use NSNotificationCenter to fire a notification when you touch outside of the text fields bounds to which to your text fields respond to with resignFirstResponder.
Edit with more detail:
You could implement the following method on your UIViewController/View a
Its a bit of a cheap way to pull it off but does work.
您可以尝试子类化 UITextField 并覆盖 pointInside。检查收到的触摸,如果它位于 UITextField 子类实例之外,则退出FirstResponder。
编辑:
以上实际上行不通,我对不好的建议表示歉意。我建议在窗口上添加一个透明的子视图,它会覆盖 hitTest 并调用预定义的回调,但返回 nil。在回调中,您可以执行一些任意任务(例如从文本视图中退出第一响应者),然后从层次结构中删除该视图。
You might try subclassing UITextField and overriding pointInside. Examine the touch received and if it is outside of your UITextField subclass instance, then resignFirstResponder.
Edit:
The above actually won't work and I apologize for the bad advice. What I would suggest instead, is adding a transparent subview over the window, which overrides hitTest and calls a predefined callback but returns nil. In the callback, you could perform some arbitrary task (like resigning first responder from your text view) and then remove the view from the hierarchy.
我的开源项目中有一个名为
CWPrimaryViewWindow
的类,位于 https://github.com/ Jayway/CWUIKit 是用于实现此行为的辅助类。主要思想是子类化
UIWindow
以劫持所有触摸事件。然后禁止所有不针对主视图的触摸事件,而不是触发取消通知。正常情况下将触摸事件传递到主视图。There is a class named
CWPrimaryViewWindow
in my open source project at https://github.com/Jayway/CWUIKit that is a helper class for implementing this behavior.The primary idea is to subclass
UIWindow
in order to hijack all touch events. Then inhibit all touch events not targeted for the primary view, instead firing a cancel notification. While passing through touch events to the primary view as normal.