3

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?

Aaron Vegh
  • 5,217
  • 7
  • 48
  • 75
  • 1
    I know it sounds stupid as you and me both done this a bazilion times, but are you sure you hooked up a delegate connection for every UITextfield (in this case loginEmailField and loginPasswordField). Not related to this and just to nitpick, but another thing I would do in the resignFirstResponder is to use the passed textfield property and not specific field (loginPasswordField) since they are the same. – gmogames Feb 28 '19 at 03:29
  • I confirm the delegate is connected properly. Again, this works fine when it's the on-screen keyboard. It's only when a hardware keyboard is in play that it doesn't work. – Aaron Vegh Feb 28 '19 at 20:16

2 Answers2

1

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
    }
}
rbaldwin
  • 4,581
  • 27
  • 38
  • I acknowledge this is the way to set this up. And when a hardware keyboard isn't attached, it works as advertised. But it doesn't work with a hardware keyboard on device or simulator. This answer doesn't reflect an attempt to understand why that might be. – Aaron Vegh Feb 28 '19 at 20:14
  • @AaronVegh Aaron, as stated, I have tested it with a hardware keyboard on both an iPhone and an iPad and it works as intended. Are you sure your Delegates are setup correctly? If you have them set in IB, try setting them in code and try again. – rbaldwin Feb 28 '19 at 20:17
  • 1
    I can also attest that I just tested using a hardware keyboard (mac on the simulator and a bluetooth one as well) and it also worked for me – gmogames Feb 28 '19 at 21:15
  • Well, if it were a simple matter of hooking up the delegates correctly I wouldn't be here! :-P I've unhooked the delegate connections in the Storyboard and implemented them in code. The result is the same: it works fine with the on-screen keyboard, but doesn't fire with the hardware keyboard. – Aaron Vegh Mar 01 '19 at 00:28
  • In order to isolate the issue - Try creating a new basic test project (to check whether it’s your main project that is the issue) - does the problem still occur? If so, upload the test project to GitHub and share it here, and let someone else test it on their setup (to check whether it’s your Xcode or Hardware that is the issue or not). – rbaldwin Mar 01 '19 at 04:39
  • Blank test project: everything works great, which doesn't surprise me. There's clearly something amiss in my project, but what??? – Aaron Vegh Mar 03 '19 at 01:50
  • Ok, try this (in this order): (1) Delete both TextFields outlet code in your ViewController (2) Delete both TextFields in IB, place new ones, and reconnect them to your VC (3) Make sure delegates are set in code or via IB (4) Delete derived data (sometimes causes funky behaviour) https://stackoverflow.com/questions/38016143/how-can-i-delete-derived-data-in-xcode-8 (5) Full Clean of Project command+option+shift+K. (6) Test them again. Not sure what else to suggest without seeing the actual project. – rbaldwin Mar 03 '19 at 06:25
  • Yeah, wish that could've done it, but it's the same story. – Aaron Vegh Mar 03 '19 at 15:59
0

You can listen to UIKeyboardWillHideNotification from NotificationCenter.

Shamas S
  • 7,507
  • 10
  • 46
  • 58
  • To what end? Hitting Return while in a text field does not hide the keyboard. I'm talking about an attached hardware keyboard. – Aaron Vegh Feb 28 '19 at 20:15