如何让iPhone上的回车键隐藏键盘?

107

我有两个UITextFields(例如用户名和密码),但在按下键盘的返回键时,我无法摆脱键盘。我该如何解决?

14个回答

241

首先,在您的视图/视图控制器的头文件中按照以下方式符合 UITextFieldDelegate 协议:

@interface YourViewController : UIViewController <UITextFieldDelegate>

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

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

    return YES;
}

[textField resignFirstResponder]; 会确保键盘被隐藏。

在 .m 文件中初始化textfield后,确保将你的视图/视图控制器设置为UITextField的代理。

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

7
您还可以通过在Storyboard中单击文本框,显示“实用工具”面板,单击“连接检查器”,将委托插座拖到视图控制器上来实现委托。 - abc123
1
你的textFieldShouldReturn方法实现在哪里?你需要将该类设置为UITextField的代理。只有设置为代理的类才会触发代理回调,而且必须已经实现了该方法。 - Sid
1
你在 MyViewController 的头文件中设置它符合 UITextFieldDelegate 了吗? - Sid
1
只要myTextField已经被正确分配且不为nil。技术上说,如果它是nil(不会崩溃),但那样什么也做不了 :) - Sid
1
如果我可以补充一点,即使您只是使用一个已覆盖的UITextField类,例如UIFloatLabelTextField,您仍然需要yourTextField.delegate = self;!!! - Gellie Ann
显示剩余4条评论

19

实现UITextFieldDelegate方法,像这样:

- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
    [aTextField resignFirstResponder];
    return YES;
}

8

参见管理键盘以获取有关此主题的完整讨论。


6

您的UITextFields应该有一个委托对象(UITextFieldDelegate)。在您的委托中使用以下代码使键盘消失:

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

到目前为止应该可以工作了...


嘿,伙计,我已经按照你上面说的做了,但是键盘仍然无法消失。你有什么想法吗?谢谢。 - K.Honda
嗨,克里斯,现在一切都解决了。谢谢。 - K.Honda

6

经过几次尝试,我遇到了同样的问题,以下方法对我有用:

检查您的拼写是否正确 -

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

我在textField中进行了更正,而不是textfield,将"F"大写...成功了。


4
当按下回车键时,请调用:
[uitextfield resignFirstResponder];

嗨,Conor,这个应用程序如何知道何时按下返回键?谢谢。 - K.Honda

3

将UITextField的Delegate设置为您的ViewController,添加文件所有者和UITextField之间的引用输出,然后实现此方法:

-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{
   if (textField == yourTextField) 
   {
      [textField resignFirstResponder]; 
   }
   return NO;
}

3
请使用这个代替预定义的类。
class ViewController: UIViewController, UITextFieldDelegate {

点击键盘外部以关闭键盘
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }

并在按下回车键时移除键盘

在viewDidLoad()方法中添加以下代码:

inputField是所使用的textField的名称。

self.inputField.delegate = self

并添加此功能。
func textFieldShouldReturn(textField: UITextField) -> Bool {        
        textField.resignFirstResponder()        
        return true        
    }

3

经过一番寻找,我终于整理出了一些有意义的内容,并且它们非常有效。

.h

//
//  ViewController.h
//  demoKeyboardScrolling
//
//  Created by Chris Cantley on 11/14/13.
//  Copyright (c) 2013 Chris Cantley. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>

// Connect your text field to this the below property.
@property (weak, nonatomic) IBOutlet UITextField *theTextField;

@end

.m

//
//  ViewController.m
//  demoKeyboardScrolling
//
//  Created by Chris Cantley on 11/14/13.
//  Copyright (c) 2013 Chris Cantley. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
    // _theTextField is the name of the parameter designated in the .h file. 
    _theTextField.returnKeyType = UIReturnKeyDone;
    [_theTextField setDelegate:self];

}

// This part is more dynamic as it closes the keyboard regardless of what text field 
// is being used when pressing return.  
// You might want to control every single text field separately but that isn't 
// what this code do.
-(void)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
}


@end

希望这可以帮到您!

2

在Swift中,你应该委托UITextFieldDelegate,这很重要,不要忘记在viewController中像这样:

class MyViewController: UITextfieldDelegate{

     mytextfield.delegate = self

     func textFieldShouldReturn(textField: UITextField) -> Bool {
          textField.resignFirstResponder()
     }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接