Swift 4 升级后未调用 textFieldShouldReturn
textFieldShouldReturn not being called after upgrade in Swift 4
我有一个视图控制器,上面有几个 UITextField
。当 UITextField
获得焦点时,如果我点击键盘上的 Return 键,我会转到下一个 UITextField
。如果下一个 UITextField
在键盘下方,我将视图向上移动。
一切都很好,直到昨天我将应用程序升级到 Swift 4,经过一些更改后。现在它不再起作用了。我加载了 Swift 3 版本,它工作得很好。问题是我看不出有什么不同,也想不通。
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 升级的原因是我什至没有碰这个 class,其他一切正常。
有什么我遗漏的吗?
textFieldShouldReturn
是一个 UITextFieldDelegate
函数,但是您的 class 符合 UITextViewDelegate
。将一致性替换为 UITextFieldDelegate
:
class ServerWizardVC: UIViewController, UITextFieldDelegate, UIDocumentMenuDelegate, UIDocumentPickerDelegate, FileManagerDelegate
我有一个视图控制器,上面有几个 UITextField
。当 UITextField
获得焦点时,如果我点击键盘上的 Return 键,我会转到下一个 UITextField
。如果下一个 UITextField
在键盘下方,我将视图向上移动。
一切都很好,直到昨天我将应用程序升级到 Swift 4,经过一些更改后。现在它不再起作用了。我加载了 Swift 3 版本,它工作得很好。问题是我看不出有什么不同,也想不通。
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 升级的原因是我什至没有碰这个 class,其他一切正常。
有什么我遗漏的吗?
textFieldShouldReturn
是一个 UITextFieldDelegate
函数,但是您的 class 符合 UITextViewDelegate
。将一致性替换为 UITextFieldDelegate
:
class ServerWizardVC: UIViewController, UITextFieldDelegate, UIDocumentMenuDelegate, UIDocumentPickerDelegate, FileManagerDelegate