53

(iOS8, Xcode6, Swift) Using Swift, how do I capture a tap on the "Return" button?

The doc at the following link specifies using the textFieldShouldReturn method:

// Swift
@optional func textFieldShouldReturn(_ textField: UITextField!) -> Bool

Where I'm hung up is in the "_ textField" part. I've created the text field using Storyboard. How do I capture notifications for this specific text field? Do I need to create a new class and set it as a delegate for this text field? Do I assign the text filed a name, and then somehow hook into it?

https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn

Naresh
  • 16,698
  • 6
  • 112
  • 113
kmiklas
  • 13,085
  • 22
  • 67
  • 103

4 Answers4

95
class ViewController: UIViewController,UITextFieldDelegate //set delegate to class

@IBOutlet var txtValue: UITextField //create a textfile variable

override func viewDidLoad() {
   super.viewDidLoad() 
   txtValue.delegate = self //set delegate to textfile 
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {   //delegate method
   textField.resignFirstResponder()
   return true
}
Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
jayesh kavathiya
  • 3,531
  • 2
  • 22
  • 25
  • 7
    Please don't post the same badly formatted answer to multiple questions. If they can be answered by the same thing, they're duplicates and should be flagged/closed as such. – jrturton Jul 07 '14 at 06:26
  • This assumes the UITextField was created in a Storyboard. At least in my case, I am creating all my UITextFields programatically. – Alex Nov 15 '14 at 00:27
  • 1
    that's not make any different if you create UITextFields programatically or via storybord or XIB. here only you need to set delegate to textfield than it's work with all – jayesh kavathiya Nov 15 '14 at 06:48
  • If you don't want the keyboard to hide on return key. no need for `textField.resignFirstResponder()` – Ali Nov 30 '17 at 00:10
25

Implement this function

func textFieldShouldReturn(_ textField: UITextField) -> Bool {   //delegate method
   textField.resignFirstResponder()
   return true
}

And for delegate you can set using the Utilities pane / Connections Inspector / delegate and then drag to ViewController (yellow button in the storyboard)

Then you do not need to set the delegate programmatically for every text field

herau
  • 1,466
  • 2
  • 18
  • 36
sijones
  • 429
  • 4
  • 10
6

In Swift 4.2 and Xcode 10.1

//UITextField delegate method
func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    if textField == TF1 {
        textField.resignFirstResponder()//
        TF2.becomeFirstResponder()//TF2 will respond immediately after TF1 resign.
    } else if textField == TF2  {
        textField.resignFirstResponder()
        TF3.becomeFirstResponder()//TF3 will respond first
    } else if textField == TF3 {
        textField.resignFirstResponder()
    }
    return true
}
Naresh
  • 16,698
  • 6
  • 112
  • 113
3

You need to set an object as the text field's delegate. Usually that would be the view controller that the text field exists in. You don't need to inherit from any other class, or, strictly speaking, implement a delegate (but you could implement UITextFieldDelegate to make things a little clearer.)

Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
  • I set the containing view controller as the delegate for the text field. I then entered the following function into ViewController.swift but the event is not firing when "Go" is tapped. What should be entered where "asdf" is in this function: func textFieldShouldReturn(asdf: UITextField!) -> Bool { NSLog("Yes") return true } – kmiklas Jun 10 '14 at 22:04
  • This worked, but I don't understand how iOS knows to run this function for the text field in question. What if there are multiple text fields on a page, all with ViewController.swift set as their delegate? func textFieldShouldReturn(textField: UITextField!) -> Bool { NSLog("Yes") return true } – kmiklas Jun 10 '14 at 22:08
  • hey @kmiklas - you know "which one" it is, from the argument that arrives. you'd just compare that to your IBOutlets, if that is necessary – Fattie Oct 21 '16 at 22:30