iPad 关闭键盘而不知道哪个文本字段打开了它
有没有办法做一个通用的resignFirstResponder
来隐藏键盘,无论文本字段/视图/等调用它是什么?
原因是我的视图上有很多文本字段,并且不想让所有文本字段都 resignFirstResponder
来隐藏键盘。只是想要一个通用的
[self resignFirstResponder]
。
有什么想法吗?
提前致谢
Is there a way to do a general resignFirstResponder
to hide the keyboard regardless of what textfield/view/etc calls it?
Reason is I have a lot of textfields on my view and don't want to have to resignFirstResponder
for all textfields to hide the keyboard. Just want a general
[self resignFirstResponder]
.
Any ideas?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道这已经被标记为已回答,但是对于那些像我一样遇到这种情况的人,您可以在包含文本字段的视图上使用以下方法。
它将隐藏键盘当我们点击查看时。
I know that this has already been marked as answered, but for those that run into this like I did you can just use the following method on the view that contains the textfields.
it will hide keyboard when we click on view.
您可以借助以下代码在不引用 UITextfield / UITextView 的情况下关闭键盘:
这将在没有引用的情况下全局关闭键盘。
希望这会对您有所帮助。
You can dismiss the keyboard without any reference to UITextfield / UITextView with help of below code:
this will dismiss the keyboard globally without the reference.
hope this will help you.
最简单的方法是有一个方法,每当您想要关闭键盘时,如下所示:
The easiest way to do this is to have a method for whenever you want to dismiss the keyboard that looks like this:
您可以检查以下问题:
总结:
您可以对您选择的其他事物调用BecomeFirstResponder。它可以是 UIViewController 或 UIView。我以前也遇到过类似的问题,当我将视图控制器推回其调用者时,我需要让键盘消失,而不知道哪个文本字段是第一响应者。然后,在我返回的视图控制器的 viewWillAppear 上,我调用了 [self comeFirstResponder] 并且推送视图的键盘消失了。因为这使得无论哪个文本字段松动都是第一个响应者
You can check these questions:
In summary:
You can call becomeFirstResponder on some other thing that you choose. It could be a UIViewController or a UIView. I had a similar problem before, I needed to make my keyboard go away when I was pushing my view controller back to its caller, without knowing which textfield was the first responder. Then, on viewWillAppear of my view controller which I was returning back, I called [self becomeFirstResponder] and the keyboard of the pushed view was gone. Because this made whichever text field was it loose being the first responder
在我自己的应用程序中,当我有多个文本字段并且希望让键盘消失而不管哪个字段调用它时,我只需编写一个方法并让每个文本字段
resignFirstResponder
。我认为作为一名程序员,您应该清楚地了解视图控制器上有多少文本字段以及如何访问它们,否则它会变得混乱并且您的应用程序看起来不会很好...:-P
In my own app when I had more than one text field and would like to make the keyboard go away regardless which of the fields called it, I would just wrote a method and let each and every of them
resignFirstResponder
.I assume that as a programmer, you should have the clear knowledge how many text fields are on your view controller and how you can access them, otherwise it'll get messed up and you app won't look good... :-P