ios 在 UITextField 中按回车键时如何隐藏键盘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3573955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:37:27  来源:igfitidea点击:

How to hide the keyboard when I press return key in a UITextField?

iosobjective-ccocoa-touchdelegates

提问by aden

Clicking in a textfield makes the keyboard appear. How do I hide it when the user presses the return key?

单击文本字段会使键盘出现。当用户按下返回键时如何隐藏它?

回答by Saurabh

First make your file delegate for UITextField

首先为 UITextField 创建文件委托

@interface MYLoginViewController () <UITextFieldDelegate>

@end

Then add this method to your code.

然后将此方法添加到您的代码中。

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

Also add self.textField.delegate = self;

还添加 self.textField.delegate = self;

回答by oscar castellon

In viewDidLoaddeclare:

viewDidLoad声明中:

[yourTextField setDelegate:self];

[yourTextField setDelegate:self];

Then, include the override of the delegate method:

然后,包括委托方法的覆盖:

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

回答by Zaid Pathan

Try this in Swift,

Swift 中试试这个,

Step 1:Set delegate as self to your textField

第 1 步:将委托设置为 self 到您的textField

textField.delegate = self

Step 2:Add this UITextFieldDelegate below your class declaration,

第 2 步:在您的类声明下方添加此 UITextFieldDelegate,

extension YourClassName: UITextFieldDelegate {
    func textFieldShouldReturn(textField: UITextField) -> Bool {
         textField.resignFirstResponder()
        return true
    }
}

回答by Homam

In swift do like this:
First in your ViewControllerimplement this UITextFieldDelegateFor eg.

在 swift 中这样做:
首先在你的ViewController实现中,UITextFieldDelegate例如。

class MyViewController: UIViewController, UITextFieldDelegate {
....
}

Now add a delegate to a TextFieldin which you want to dismiss the keyboard when return is tapped either in viewDidLoadmethod like below or where you are initializing it. For eg.

现在将一个委托添加到 aTextField中,当您在viewDidLoad下面的方法中或您正在初始化它的地方点击 return 时,您希望在其中关闭键盘。例如。

override func viewDidLoad() {

    super.viewDidLoad()   
    myTextField.delegate = self
}

Now add this method.

现在添加这个方法。

func textFieldShouldReturn(textField: UITextField) -> Bool {

    textField.resignFirstResponder()
    return true
}

回答by Vineesh TP

Try this,

尝试这个,

[textField setDelegate: self];

Then, in textField delegate method

然后,在 textField 委托方法中

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

回答by Matrix

set delegate of UITextField, and over ride, textFieldShouldReturnmethod, in that method just write following two lines:

设置委托UITextField并覆盖textFieldShouldReturn方法,在该方法中只需编写以下两行:

[textField resignFirstResponder];
return YES;

that's it. Before writing a code dont forget to set delegate of a UITextFieldand set Return key type to "Done" from properties window.(command + shift + I).

就是这样。在编写代码之前,不要忘记设置 a 的委托UITextField并将返回键类型从属性窗口设置为“完成”。(命令 + shift + I)。

回答by Ferenc Kiss

You can connect "Primary Action Triggered" (right click on UITextField) with an IBAction and you can resign first responder (without delegation). Example (Swift 4):

您可以将“Primary Action Triggered”(在 UITextField 上右键单击)与 IBAction 连接起来,并且您可以退出第一响应者(无需委托)。示例(Swift 4):

@IBAction func textFieldPrimaryAction(_ sender: UITextField) {
    sender.resignFirstResponder()
    ...
}

回答by dimpiax

Swift 4

斯威夫特 4

Set delegate of UITextFieldin view controller, field.delegate = self, and then:

UITextField在视图控制器中设置委托field.delegate = self,然后:

extension ViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // don't force `endEditing` if you want to be asked for resigning
        // also return real flow value, not strict, like: true / false
        return textField.endEditing(false)
    }
}

回答by Graycodder

If you want to hide the keyboard for a particular keyboard use [self.view resignFirstResponder];If you want to hide any keyboard from view use [self.view endEditing:true];

如果您想隐藏特定键盘使用的键盘 [self.view resignFirstResponder];如果您想隐藏任何键盘以供查看使用[self.view endEditing:true];

回答by DURGESH

[textField resignFirstResponder];

Use this

用这个