Search code examples
iosswiftuitextfieldswift4uitextfielddelegate

textFieldShouldReturn not being called after upgrade in Swift 4


I have a view controller with a few UITextFields on it. When a UITextField has focus, if I tap on the Return key on the keyboard, I go to the next UITextField. If the next UITextField is below the keyboard, I move the view up.

All was fine until yesterday when I upgraded the app to Swift 4, after a few changes. Now it's no longer working. I loaded the Swift 3 version and it's working just fine. The problem is I don't see any difference and I can't figure it out.

UITextFields with Delegates

class ServerWizardVC: UIViewController, UITextViewDelegate, UIDocumentMenuDelegate, UIDocumentPickerDelegate, FileManagerDelegate

@IBOutlet weak var tfServerURL: UITextField!
@IBOutlet weak var tfServerUser: UITextField!
@IBOutlet weak var tfServerPassword: UITextField!
@IBOutlet weak var tfServerPort: UITextField!

override func viewDidLoad()
{
    print("ServerWizardVC > viewDidLoad")

    super.viewDidLoad()

    tfServerURL.tag = 0

    registerForKeyboardNotifications()
    deregisterFromKeyboardNotifications()
}

override func viewWillAppear(_ animated: Bool)
{
    print("ServerWizardVC > viewWillAppear")

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}

func registerForKeyboardNotifications()
{
    print("ServerWizardVC > registerForKeyboardNotifications")

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}

func deregisterFromKeyboardNotifications()
{
    print("ServerWizardVC > deregisterFromKeyboardNotifications")

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

@objc func keyboardWillShow(notification:NSNotification)
{
    print("ServerWizardVC > keyboardWillShow")

    let userInfo:NSDictionary = notification.userInfo! as NSDictionary
    let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height

    keyboardHeightValue = keyboardHeight
}

@objc func keyboardWasShown(notification: NSNotification)
{
    print("ServerWizardVC > keyboardWasShown")
}

@objc func keyboardWillBeHidden (notification: NSNotification)
{
    print("ServerWizardVC > keyboardWillBeHidden")
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
    print("ServerWizardVC > textFieldShouldReturn")

    if let nextField = tfServerURL.superview?.viewWithTag(textField.tag + 1) as? UITextField
    {
        nextField.becomeFirstResponder()

        checkTextFieldPosition(tfTag: textField.tag + 1)
    }
    else
    {
        vMainView.frame.origin.y = 0

        textField.resignFirstResponder()
    }

    return false
}

func checkTextFieldPosition(tfTag : Int)
{
    print("ServerWizardVC > checkTextFieldPosition")

    let keyboardTop = mainViewHeight - keyboardHeightValue
    let tfServerPasswordTop =  tfServerPassword.frame.origin.y

    if(tfTag == 3)
    {
        if((keyboardTop < tfServerPasswordTop) && (vMainView.frame.origin.y == 0))
        {
            let yPosition = vMainView.frame.origin.y - keyboardHeightValue + 100
            vMainView.frame.origin.y = yPosition
        }
    }
}

The reason I blame this on the Swift 4 upgrade is that I didn't even touch this class and everything else works fine.
Is there anything I'm missing?


Solution

  • textFieldShouldReturn is a UITextFieldDelegate function, but your class conforms to UITextViewDelegate instead. Replace the conformance with UITextFieldDelegate:

    class ServerWizardVC: UIViewController, UITextFieldDelegate, UIDocumentMenuDelegate, UIDocumentPickerDelegate, FileManagerDelegate