当用户点击其他地方时如何使键盘消失?

发布于 2024-10-06 06:31:36 字数 643 浏览 7 评论 0原文

可能的重复:
通过触摸 UITableView 背景关闭键盘

如何让键盘消失当用户点击其他地方时离开?

注意:我知道如何通过将 resignFirstResponder 命令发送到 UITextField 来使键盘消失。目前,“完成”按钮已连接到所有正确的代码来执行此操作并且有效。

我有一个带有不同 UITableViewCells 的 UITableView,如果用户移动到另一个单元格,我希望键盘消失。

那么我还需要在哪些事件中包含 resignFirstResponder 才能使键盘消失。

假设 UITableViewCell A 有 UITextField,UITableViewCell B 有一个按钮。如果用户按下单元格 B 中的按钮,那么我需要将命令 resignFirstResponder 发送回单元格 A 中的 UITextField。首先,按钮不知道应该将命令发送到哪个单元格,其次,即使按钮确实知道将命令发送到哪个单元格怎么办?

Possible Duplicate:
Dismiss keyboard by touching background of UITableView

How do I make the keyboard go away when the user clicks somewhere else?

Note: I know how to make the keyboard disappear with sending the resignFirstResponder command to the UITextField. At present the "Done" button is connected to all the correct code to do this and this works.

I have a UITableView with different UITableViewCells, and if the user moves onto another cell I want the keyboard to disappear.

So what events do I also need to include the resignFirstResponder in, for the keyboard to disappear.

Suppose UITableViewCell A has the UITextField, and UITableViewCell B has a button. If the user presses the button in cell B, then I will need to send the command resignFirstResponder back to the UITextField in cell A. First of all the button has no idea which cell it should sent the command to, and second even if the button did know which cell to send the command to how would it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

残疾 2024-10-13 06:31:37

没有简单的方法可以做到这一点。您可以在文本字段周围放置一组透明的“屏蔽视图”,占据屏幕的其余部分,并使用它们上的任何触摸来关闭键盘。

There's no trivial way to do this. You can put a transparent set of "shield views" all the way around the text field that take up the rest of the screen, and use any touches on them to dismiss the keyboard.

肤浅与狂妄 2024-10-13 06:31:37

您可以创建一个通用的“hideKeyboard”方法,其中可以包含可以作为第一响应者的所有文本字段。例如,

-(void) hideKeyboard {
     [textFieldName resignFirstResponder];
     [textFieldSurname resignFirstResponder];
     for (UITextField * txtField in arrTextFields) {
         [txtField resignFirstResponder];
      }
}

然后,在班级的各个部分,根据所需的功能,调用;

[self hideKeyBoard];

这种简单的方法意味着您无需跟踪“具有焦点”/第一响应者状态的单个项目。

如何触摸屏幕的任何部分以使键盘消失
要触摸 UITableView 外部的某个位置并使键盘消失,请将一个不可见的按钮放在您想要响应的“触摸区域”顶部。然后,只需从该不可见按钮的触摸事件中调用 [self hideKeyboard] 即可。使用 IB,将一个新的圆形按钮拖到您的视图上,然后调整其大小以占据整个屏幕大小。接下来,在 IB 文档窗口中的控件列表中向上或向下拖动按钮,以便该按钮位于所有文本字段和按钮的后面,但位于其他任何内容(如图像等)的前面。最后,将按钮的类型更改为“自定义”以使其不可见,但仍响应事件。现在您所要做的就是连接新按钮的“touch up inside”事件以触发“hideKeyboard”方法。

此外,请参阅这篇文章,了解当上述解决方案不起作用时关闭键盘的出色解决方案:stackoverflow Question 1823317

You can create a generic 'hideKeyboard' method in which you can include all text fields that can be first responders. For example,

-(void) hideKeyboard {
     [textFieldName resignFirstResponder];
     [textFieldSurname resignFirstResponder];
     for (UITextField * txtField in arrTextFields) {
         [txtField resignFirstResponder];
      }
}

Then, at various sections in your class, depending on the functionality required, call;

[self hideKeyBoard];

This simple method means you won't need to keep track of the individual item that 'has the focus' / first responder status.

How to touch any part of the screen to make the keyboard go away
To touch somewhere outside the UITableView and have the keyboard disappear, place an invisible button on top of the 'touch area' that you want to respond to. Then, simply call [self hideKeyboard] from the touch event for that invisible button. Using IB, drag a new rounded button onto your view, then size it to take up the full size of the screen. Next,drag the button up or down the controls list in the IB document window so that button is behind all text fields and buttons, but in front of anything else (like images etc.). Finally, change the type of the button to 'Custom' to make it invisible, but still respond to events. Now all you have to do is to connect the new button's 'touch up inside' event to trigger the 'hideKeyboard' method.

Additionally, see this post for a brilliant solution to dismiss the keyboard when the above solution doesn't work : stackoverflow question 1823317

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