Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField Delegate textFieldShouldReturn Not Called with Hardware Keyboard Return

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?

like image 448
Aaron Vegh Avatar asked Jan 27 '26 21:01

Aaron Vegh


1 Answers

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
    }
}
like image 189
rbaldwin Avatar answered Jan 30 '26 17:01

rbaldwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!