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

前端 未结 12 1413
野趣味
野趣味 2020-12-04 07:31

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

相关标签:
12条回答
  • 2020-12-04 08:01

    In swift do like this:
    First in your ViewController implement this UITextFieldDelegate For eg.

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

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

    override func viewDidLoad() {
    
        super.viewDidLoad()   
        myTextField.delegate = self
    }
    

    Now add this method.

    func textFieldShouldReturn(textField: UITextField) -> Bool {
    
        textField.resignFirstResponder()
        return true
    }
    
    0 讨论(0)
  • 2020-12-04 08:06
    [textField resignFirstResponder];
    

    Use this

    0 讨论(0)
  • 2020-12-04 08:08

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

    @IBAction func textFieldPrimaryAction(_ sender: UITextField) {
        sender.resignFirstResponder()
        ...
    }
    
    0 讨论(0)
  • 2020-12-04 08:12

    Try this in Swift,

    Step 1: Set delegate as self to your textField

    textField.delegate = self
    

    Step 2: Add this UITextFieldDelegate below your class declaration,

    extension YourClassName: UITextFieldDelegate {
        func textFieldShouldReturn(textField: UITextField) -> Bool {
             textField.resignFirstResponder()
            return true
        }
    }
    
    0 讨论(0)
  • 2020-12-04 08:13

    Ok, I think for a novice things might be a bit confusing. I think the correct answer is a mix of all the above, at least in Swift4.

    Either create an extension or use the ViewController in which you'd like to use this but make sure to implement UITextFieldDelegate. For reusability's sake I found it easier to use an extension:

    extension UIViewController : UITextFieldDelegate {
        ...
    }
    

    but the alternative works as well:

    class MyViewController: UIViewController {
        ...
    }
    

    Add the method textFieldShouldReturn (depending on your previous option, either in the extension or in your ViewController)

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        return textField.endEditing(false)
    }
    

    In your viewDidLoad method, set the textfield's delegate to self

    @IBOutlet weak var myTextField: UITextField!
    ...
    
    override func viewDidLoad() {
        ..
        myTextField.delegte = self;
        ..
    }
    

    That should be all. Now, when you press return the textFieldShouldReturn should be called.

    0 讨论(0)
  • 2020-12-04 08:15

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

    [textField resignFirstResponder];
    return YES;
    

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

    0 讨论(0)
提交回复
热议问题