我想从用户在文本字段中输入文本的主视图进行非常简单的转换,然后让它的segue转到下一个视图控制器。我确实实现了函数textfieldshouldfreturn(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
    }
}

任何快速的调试帮助,这将是非常感谢。我已经花了很多时间来修理,但我还没找到出路!!!

最佳答案

代码中有一些错误:
要能够调用textFieldShouldReturn,您需要从UITextFieldDelegate继承并设置您的textFieldlogInField.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
    }
}

10-08 11:31