如何在iPhone上制作返回键使键盘消失? [英] How to make return key on iPhone make keyboard disappear?

查看:125
本文介绍了如何在iPhone上制作返回键使键盘消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 UITextFields (例如用户名和密码),但是当按下键盘上的返回键时我无法摆脱键盘。我怎样才能做到这一点?

I have two UITextFields (e.g. username and password) but I cannot get rid of the keyboard when pressing the return key on the keyboard. How can I do this?

推荐答案

首先,您需要在视图中符合 UITextFieldDelegate 协议/ ViewController的头文件如下:

First you need to conform to the UITextFieldDelegate Protocol in your View/ViewController's header file like this:

@interface YourViewController : UIViewController <UITextFieldDelegate>

然后在你的.m文件中你需要实现以下 UITextFieldDelegate 协议方法:

Then in your .m file you need to implement the following UITextFieldDelegate protocol method:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}

[textField resignFirstResponder]; 确保键盘被解除。

确保在.m中初始化文本字段后,将view / viewcontroller设置为UITextField的委托: / p>

Make sure you're setting your view/viewcontroller to be the UITextField's delegate after you init the textfield in the .m:

yourTextField = [[UITextField alloc] initWithFrame:yourFrame];
//....
//....
//Setting the textField's properties
//....    
//The next line is important!!
yourTextField.delegate = self; //self references the viewcontroller or view your textField is on

这篇关于如何在iPhone上制作返回键使键盘消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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