Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the return key to perform the same action as a button press in Swift?

Tags:

xcode

swift

ios8

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?

like image 394
Fiducial13 Avatar asked Oct 09 '14 21:10

Fiducial13


People also ask

How do I get the return option on my keyboard?

If your keyboard doesn't have a dedicated Enter key, you can type the Enter key by pressing Fn-Return. That's why some Return keys have “Enter” printed in small type above the word “Return”.


1 Answers

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 } 
like image 105
Steve Rosenberg Avatar answered Sep 19 '22 07:09

Steve Rosenberg