在Swift 4中升级后,没有调用textFieldShouldReturn

时间:2018-02-07 11:25:46

标签: ios swift uitextfield swift4 uitextfielddelegate

我有一个视图控制器,上面有几个UITextField个。当UITextField有焦点时,如果我点击键盘上的Return键,我会转到下一个UITextField。如果下一个UITextField位于键盘下方,我会向上移动视图。

直到昨天我将应用程序升级到Swift 4后,一切都很好。现在它不再工作了。我加载了Swift 3版本,它运行得很好。问题是我没有看到任何差异,我无法弄清楚。

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
        }
    }
}

我在Swift 4升级上归咎于此的原因是我甚至没有触及这个课程,其他一切工作正常。
我有什么遗失的吗?

1 个答案:

答案 0 :(得分:3)

textFieldShouldReturnUITextFieldDelegate函数,但您的类符合UITextViewDelegate。替换为UITextFieldDelegate

的一致性
class ServerWizardVC: UIViewController, UITextFieldDelegate, UIDocumentMenuDelegate, UIDocumentPickerDelegate, FileManagerDelegate