The textFieldShouldReturn
function is not being called at all: there are no errors but the keyboard does not respond at all.
My case is different from How to hide keyboard in swift on pressing return key? as in my case nothing is happening at all and other cases are in Objective-C.
Here is my code:
import UIKit
class ViewController: UIViewController {
@IBOutlet var textField: UITextField!
func textFieldShouldReturn(textField: UITextField) -> Bool {
resignFirstResponder()
return true
}
}
textField is an outlet to a text field on my storyboard. I also tried self.endEditing
instead of resignFirstResponder
.
The rest of this answer is still very useful, and I'll leave it there as it can potentially help other askers... but here, I missed the obvious problem with this specific example...
We're not calling resignFirstResponder
on the text field. We're calling it on the view controller. We need to call it on the text field, so modify your code to look like this:
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
A UITextField
will only call the textFieldShouldReturn
property on the object which is its delegate.
We can fix this programmatically by adding a viewDidLoad
method to set that:
override func viewDidLoad() {
super.viewDidLoad()
self.textField.delegate = self
}
But we can also set this up via the storyboard at build time.
Right click on the textfield to check and see whether or not the delegate has been set:
If that circle next to delegate
is unfilled, we haven't set the delegate for our UITextField
yet.
To set the delegate, hover over this circle. It will change to a plus sign. Now click and drag to the view controller that you want to delegate the text field (the view controller the text field is part of).
When you've appropriately hooked the view controller up as a delegate, this menu should look like this:
If using Swift 3+, you have to add an underscore before the first property. Like:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
This is also very well documented in the Apple Documentation. https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn
I'm enrolled in a Swift 4 Udemy course, and the instructor said to add the UITextFieldDelegate class for the ViewController in addition to Cntrl - dragging from the textField to the ViewController button and selecting delegate.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
Well, in my case. I accidentally enable hardware keyboard. Make sure you unchecked "Connect to hardware keyboard" in order for the keyboard to show up in the simulator.
Hardware -> Keyboard -> Connect to hardware keyboard
Hope this will help others too!
You can set all textFields delegate in a loop:
var tF: [UITextField] = []
tf = [my1TextField, my2TextField, my3TextField]
for textField in tf {
textField.delegate = self
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With