键盘解除问题 - Objective C
我已将文本字段的委托设置为 self,并在 .h 中添加了它的委托,但我遇到了问题。如果我单击视图中除文本字段之外的任何内容,我希望键盘隐藏。这可能吗?如果是这样,我该怎么做?
I have set the delegate of my textfield to self and I have added the delegate for it in the .h, but I have a problem. I want the keyboard to hide If I click anything but the textfield in the view. Is this possible? If so, how would I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了一种更简单的隐藏键盘的方法,当您单击屏幕方式时它就可以工作。
I found a simpler way to hide the keyboard and it works when you click on the screen way.
您可以做一些“黑客攻击”...就像这样:
所以基本上您所做的就是在文本字段下方添加一个虚拟按钮。因此,当您触摸除 textField 之外的其他任何内容时,他将使您的 textField resignFirstResponder 并将自己从视图中删除。
编辑1(tweek)你只需要替换这个:
为此:
You can do some "hacking"... Like this:
So basically what you do, is to add a dummy button right bellow your textField. So when you touch anything else, excepts your textField, he will make your textField resignFirstResponder and removes himself from the view.
Edit 1 ( the tweek) You just need to replace this:
for this:
您只需要实现以下委托方法:
You just need to implement the following delegate methods:
如果您有
UITextField
的 IBOutlet,则可以使用[textField resignFirstResponder];
关闭键盘。话虽如此,您必须为视图上的其他所有内容实现事件侦听器。如果您希望当用户点击视图的背景时隐藏键盘,可以通过视图控制器实现中的触摸事件来完成:如果您有任何按钮或子视图,则必须实现单独的按钮或子视图也可以触摸这些事件或操作。
If you have an IBOutlet for the
UITextField
, you can dismiss the keyboard using[textField resignFirstResponder];
. This being said, you will have to implement event listeners for everything else on the view. If you want the keyboard to hide when the user taps the view's background, it can be done through a touch event in the view controller's implementation:If you have any buttons or subviews, you will have to implement individual touch events or actions for those as well.