如何在iphone上触摸空白区域时隐藏键盘 [英] how to hide the keyboard when empty area is touched on iphone

查看:112
本文介绍了如何在iphone上触摸空白区域时隐藏键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常当您触摸文本输入区域时,键盘会弹出,当您触摸屏幕的空白区域时,键盘会消失。如何实现这一目标?

normally when you touch the text input area, the keyboard pops up and when you touch the empty area of screen, the keyboard disappeared. How to make that happen?

就像我们在iphone safari上所经历的那样...

just like what we experienced on iphone safari...

谢谢

推荐答案

更新方式(推荐):

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
   [self.view endEditing:YES];
}

这将结束所有子视图的编辑并辞退第一响应者。

This will end editing on all subviews and resign the first responder.

其他方式(枚举所有文本视图):

Other way (enumerating over all text views):

这是一步一步:

向视图控制器添加IBAction,例如 - (IBAction)backgroundTouch:(id)sender

Add an IBAction to your view controller, such as - (IBAction)backgroundTouch:(id)sender

在backgroundTouch操作中,您需要将resignFirstResponder消息发送到视图中的所有文本框。这是不幸的,但是必要的,因为目前无法检索当前具有FirstResponder状态的对象。它看起来应该是这样的:

In the backgroundTouch action, you need to send the resignFirstResponder message to all of the text boxes in your view. This is unfortunate but necessary since there's currently no way to retrieve the object that currently has FirstResponder status. It should look something like this:

- (IBAction)backgroundTouch:(id)sender {
  [someTextBox resignFirstResponder];
  [anotherTextBox resignFirstResponder];
}

为视图添加一个按钮控件,调整大小以覆盖整个可见区域(状态栏或任何选项卡或导航控制器除外)。选择按钮,然后转到布局菜单并选择发送至返回。同时将按钮的类型设置为自定义,如果您没有专门为其提供任何绘图代码,则该类型不可见。

Add a button control to the view, size it to cover the entire visible area (except for the status bar or any tab or navigation controllers). Select the button and then go to the Layout Menu and select Send To Back. Also set the button's Type to custom, which is invisible if you don't specifically supply any drawing code for it.

将Button的Touch Up Inside事件连接到backgroundTouch:行动并试一试。

Connect the Button's Touch Up Inside event to the backgroundTouch: action and try it out.

这篇关于如何在iphone上触摸空白区域时隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆