iphone,当触摸UITextField外部时关闭键盘 [英] iphone, dismiss keyboard when touching outside of UITextField

查看:125
本文介绍了iphone,当触摸UITextField外部时关闭键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道当用户触摸UITextField外面时如何使键盘消失?

解决方案

您需要添加一个UITapGestureRecogniser并将其分配给视图,然后在文本字段上调用resign first responder



代码:



在viewDidLoad b
$ b

  UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard) 

[self.view addGestureRecognizer:tap];



In dismissKeyboard:


$ b b

   - (void)dismissKeyboard 
{
[aTextField resignFirstResponder];
}

(其中aTextField是负责键盘的文本字段)


I'm wondering how to make the keyboard disappear when the user touches outside of the UITextField?

解决方案

You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the textfield on it's selector.

The code:

In viewDidLoad

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];

In dismissKeyboard:

-(void)dismissKeyboard 
{
    [aTextField resignFirstResponder];
}

(Where aTextField is the textfield that is responsible for the keyboard)

这篇关于iphone,当触摸UITextField外部时关闭键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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