99

I have a button and text textfield in my view. when i click on the textfield a keyboard appears and i can write on the textfield and i also able to dismiss the keyboard by clicking on the button by adding:

[self.inputText resignFirstResponder];

Now I want to enable return key of keyboard. when i will press on the keyboard keyboard will disappear and something will happen. How can I do this?

Ander
  • 3,688
  • 1
  • 22
  • 24
razibdeb
  • 1,211
  • 1
  • 10
  • 12
  • 1
    Possible duplicate: http://stackoverflow.com/questions/4761648/close-the-keyboard-on-uitextfield – wquist Jul 19 '12 at 03:13

5 Answers5

194

Ensure "self" subscribes to UITextFieldDelegate and initialise inputText with:

self.inputText.delegate = self;

Add the following method to "self":

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.inputText) {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}

Or in Swift:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if textField == inputText {
        textField.resignFirstResponder()
        return false
    }
    return true
}
lewis
  • 2,936
  • 2
  • 37
  • 72
Ander
  • 3,688
  • 1
  • 22
  • 24
14

With extension style in swift 3.0

First, set up delegate for your text field.

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

Then conform to UITextFieldDelegate in your view controller's extension

extension YourViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField == inputText {
            textField.resignFirstResponder()
            return false
        }
        return true
    }
}
Fangming
  • 24,551
  • 6
  • 100
  • 90
8

While the other answers work correctly, I prefer doing the following:

In viewDidLoad(), add

self.textField.addTarget(self, action: #selector(onReturn), for: UIControl.Event.editingDidEndOnExit)

and define the function

@IBAction func onReturn() {
    self.textField.resignFirstResponder()
    // do whatever you want...
}
Malfunction
  • 1,324
  • 4
  • 15
  • 25
0

Just add a target on textField setup function on viewDidLoad, then add its related function as selector.

override func viewDidLoad() {
    super.viewDidLoad() 
    textField.addTarget(self, action: #selector(textFieldShouldReturn(sender:)), for: .primaryActionTriggered)
}

@objc func textFieldShouldReturn(sender: UITextField) {
    textField.resignFirstResponder()
}
Ahmadreza
  • 6,950
  • 5
  • 50
  • 69
Reza Kashkoul
  • 43
  • 2
  • 7
  • This code doesn´t compile. The couples of parentheses is not complete. – Zini Jul 20 '22 at 12:34
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 20 '22 at 14:20
-1

Use Target-Action UIKit mechanism for "primaryActionTriggered" UIEvent sent from UITextField when a keyboard done button is tapped.

textField.addTarget(self, action: Selector("actionMethodName"), for: .primaryActionTriggered)
Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93