iOS 12, Xcode 10.1. I've got a view that has a UITextField. Delegate connection is hooked up to the view controller in the Storyboard. I've done this a hojillion times before.
I want to send the first responder to the next field when Return is hit on the keyboard, so the correct way to do it is like so:
extension SignInViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
switch textField {
case loginEmailField:
loginPasswordField.becomeFirstResponder()
case loginPasswordField:
loginPasswordField.resignFirstResponder()
signInTapped(sender: UIButton())
default: break
}
return true
}
}
With a hardware keyboard attached, either with my iPad Pro with Keyboard Case, or on my Mac with the Simulator, the delegate method doesn't fire. Bring up the on-screen keyboard, and it works perfectly.
I've implemented similar things before, and I've been wracking my brain to see any differences between them, to no avail. What potential causes can I look at to resolve this, and get hardware keyboards' Return keys to function?
I have tried this and textFieldShouldReturn is being called correctly when the Return/Enter key is pressed on simulator, with Mac keyboard, and with an external bluetooth keyboard on both a real iPhone and iPad. Perhaps there is an issue with your UITextFieldDelegate setup?
I have also tried connecting the delegate both in code (as below) and via Interface Builder, and both are working.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var textFieldA: UITextField!
@IBOutlet var textFieldB: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textFieldA.delegate = self
textFieldB.delegate = self
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
switch textField {
case textFieldA:
textFieldB.becomeFirstResponder()
case textFieldB:
textFieldB.resignFirstResponder()
default:
break
}
return true
}
}
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