82

I want to know how you allow an action to be made by either pressing the return key on the software keyboard or by tapping a UIButton.

The UI button is already set up to perform an IBAction.

How do I also allow users to press the return key on the keyboard to perform the same action?

Fiducial13
  • 1,103
  • 2
  • 9
  • 16

5 Answers5

122

Make sure your class extends the UITextFieldDelegate protocol

SomeViewControllerClass : UIViewController, UITextFieldDelegate

You can perform action as follows:

override func viewDidLoad() {
    super.viewDidLoad()

    self.textField.delegate = self
}

func textFieldShouldReturn(textField: UITextField) -> Bool {

    //textField code

    textField.resignFirstResponder()  //if desired
    performAction()
    return true
}

func performAction() {   
    //action events
}
csga5000
  • 4,062
  • 4
  • 39
  • 52
Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53
  • 7
    This answer is correct but your view controller has to adopt the UITextViewDelegate protocol – lborgav Jun 12 '16 at 20:11
  • Actually the view controller needs to adopt the UITextFieldDelegate protocol, and it works just fine – Sergio Ayestarán Jul 22 '17 at 00:45
  • Agreed, I'm too burnt from working. When I said "adopt the protocol" I actually meant what @csga5000 said, inherit the protocol UITextFieldDelegate as in: class myViewController: UIViewController, UITextFieldDelegate {}, thanks for the correction! – Sergio Ayestarán Aug 16 '17 at 08:17
95

UPDATE

If your deployment target is iOS 9.0 or later, you can connect the “Primary Action Triggered” event of your text field to an action, like this:

connecting primary action triggered event

ORIGINAL

Make your view controller adopt the UITextFieldDelegate protocol.

Set your text field's delegate to your view controller.

Implement textFieldShouldReturn: to call your action.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
12

Swift 4.2 :

Other approach for the textfield created programmatically and doesn't need delegate :

     MyTextField.addTarget(self, action: #selector(MyTextFielAction)
                               , for: UIControl.Event.primaryActionTriggered)

And then perform your action like below :

    func MyTextFielAction(textField: UITextField) {
       //YOUR CODE can perform same action as your UIButton
    }
Josh
  • 559
  • 5
  • 8
5

If your deployment target is iOS 9.0 or later, you can connect the “Primary Action Triggered” event of your text field to an action, like this:

I was not able to get the "Primary Action Triggered" to work as suggested. I used "Editing Did End" and that works for now Screenshot of Editing Did End

enter image description here

Craig
  • 9,335
  • 2
  • 34
  • 38
2

Here is a complete example, with both:

  1. button-action to write and also to clear label and text when pressing button repeatedly it alternates both actions

  2. return-in-keyboard when pressing key it triggers action and also resigns first responder

class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var label1: UILabel!
    
    var buttonHasBeenPressed = false
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        textField1.delegate = self
    }
    
    
    @IBAction func buttonGo(_ sender: Any) {
        performAction()
    }
    
    
    
    
    
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        performAction()
        return true
    }
    
   
    func performAction() {
        buttonHasBeenPressed = !buttonHasBeenPressed
        
        if buttonHasBeenPressed == true {
            label1.text = textField1.text
        } else {
            textField1.text = ""
            label1.text = ""
        }
        
    }
    
}
Fausto Checa
  • 163
  • 13