用户触摸uiview时隐藏键盘 [英] Hide keyboard when user touches uiview

查看:108
本文介绍了用户触摸uiview时隐藏键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个文本框的UIView。现在我使用委托将响应者从文本字段更改为另一个。在这种情况下,当用户到达最后一个文本字段时,我的键盘会消失。

I have a UIView with multiple text boxes. Now i have used delegates to change the responder from from text field to another. In this case my key board goes away when the user comes to last text field.

但是现在我想在用户触摸UIView时隐藏我的键盘(触及任何地方在屏幕上除了文本框)。有人可以帮我这个。

but now i want to hide my key board when the user touches UIView(touches any where on screen apart from text boxes). Can some one help me with this.

谢谢

推荐答案

使用 resignFirstResponder ,如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [self.myTextField resignFirstResponder];
    }
}

您可能需要在多个文本字段中调用此项你不确定焦点目前在哪里;我没有测试过这种情况。

You may need to call this on multiple text fields if you're not sure where the focus is currently located; I haven't tested that case.

此外,在Interface Builder中,您需要为视图启用启用用户交互复选框,或者以编程方式使用:

Additionally, in Interface Builder, you'll need to enable the "User Interaction Enabled" checkbox for the view, or programatically use:

myView.userInteractionEnabled = YES;

这篇关于用户触摸uiview时隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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