我希望从用户在文本字段中输入文本的主视图中进行非常简单的转换,然后将其segue转到下一个视图控制器。我确实实现了函数 textFieldShouldReturn(textField:UITextField) - > Bool 试图执行segue,但没有得到理想的结果。 这是代码的一部分
class LogInVC: UIViewController {
@IBOutlet weak var logInLabel: UILabel!
@IBOutlet weak var logInField: UITextField!
override func viewDidLoad() {
NSLog("LogIn view loaded, setting delegate")
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
NSLog("viewWillAppear: Performing possible queued updates")
textFieldShouldReturn(textField: logInField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let autoCheckInVC = segue.destination as?
AutocheckInVC else {
preconditionFailure("Wrong destination type: \(segue.destination)")
}
guard logInField.text != nil else {
preconditionFailure("Text property should not be nil")
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.performSegue(withIdentifier: "AutoCheckInVC", sender: self)
return true
}
}
任何有关调试的快速帮助都将非常感激。我已经花了很多时间来解决这个问题,但我还没找到出路!!!
答案 0 :(得分:1)
您的代码中有一些错误:
textFieldShouldReturn
,您需要继承UITextFieldDelegate
并设置textField
,logInField.delegate = self
textFieldShouldReturn
viewDidAppear
textFieldShouldReturn
delegates
尝试使用此代码代替您的代码:
class LogInVC: UIViewController, UITextFieldDelegate {
@IBOutlet weak var logInLabel: UILabel!
@IBOutlet weak var logInField: UITextField!
override func viewDidLoad() {
NSLog("LogIn view loaded, setting delegate")
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
logInField.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
NSLog("viewWillAppear: Performing possible queued updates")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let autoCheckInVC = segue.destination as?
ViewController else {
preconditionFailure("Wrong destination type: \(segue.destination)")
}
guard logInField.text != nil else {
preconditionFailure("Text property should not be nil")
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.performSegue(withIdentifier: "AutoCheckInVC", sender: self)
return true
}
}