Swift UITextFieldShouldReturn Return Key Tap
Solution 1
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
}
Solution 2
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
Solution 3
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
}
Solution 4
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.)
kmiklas
Updated on July 09, 2022Comments
-
kmiklas almost 2 years
(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
-
kmiklas almost 10 yearsI 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 almost 10 yearsThis 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 }
-
jrturton almost 10 yearsPlease 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.
-
Alex over 9 yearsThis assumes the UITextField was created in a Storyboard. At least in my case, I am creating all my UITextFields programatically.
-
jayesh kavathiya over 9 yearsthat'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
-
Fattie over 7 yearshey @kmiklas - you know "which one" it is, from the argument that arrives. you'd just compare that to your IBOutlets, if that is necessary
-
Ali over 6 yearsIf you don't want the keyboard to hide on return key. no need for
textField.resignFirstResponder()