iPad 关闭键盘而不知道哪个文本字段打开了它

发布于 2024-10-08 01:16:26 字数 241 浏览 3 评论 0原文

有没有办法做一个通用的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悲歌长辞 2024-10-15 01:16:26

我知道这已经被标记为已回答,但是对于那些像我一样遇到这种情况的人,您可以在包含文本字段的视图上使用以下方法。

- (BOOL)endEditing:(BOOL)force

此方法查看当前视图及其子视图层次结构,以查找当前作为第一响应者的文本字段。如果找到,它会要求该文本字段辞去第一响应者的角色。如果强制参数设置为 YES,则根本不会询问文本字段;它被迫辞职。 UIView 文档

[self.view endEditing:YES];

它将隐藏键盘当我们点击查看时。

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.

- (BOOL)endEditing:(BOOL)force

This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign. UIView Documentation

[self.view endEditing:YES];

it will hide keyboard when we click on view.

橙幽之幻 2024-10-15 01:16:26

您可以借助以下代码在不引用 UITextfield / UITextView 的情况下关闭键盘:

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

这将在没有引用的情况下全局关闭键盘。

希望这会对您有所帮助。

You can dismiss the keyboard without any reference to UITextfield / UITextView with help of below code:

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

this will dismiss the keyboard globally without the reference.

hope this will help you.

年少掌心 2024-10-15 01:16:26

最简单的方法是有一个方法,每当您想要关闭键盘时,如下所示:

-(void)dismissKeyboard {
    [firstField becomeFirstResponder];
    [firstField resignFirstResponder];
}

The easiest way to do this is to have a method for whenever you want to dismiss the keyboard that looks like this:

-(void)dismissKeyboard {
    [firstField becomeFirstResponder];
    [firstField resignFirstResponder];
}
俏︾媚 2024-10-15 01:16:26

您可以检查以下问题:

总结:
您可以对您选择的其他事物调用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

歌入人心 2024-10-15 01:16:26

在我自己的应用程序中,当我有多个文本字段并且希望让键盘消失而不管哪个字段调用它时,我只需编写一个方法并让每个文本字段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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文