如何使用条件IF打开新视图

时间:2019-04-06 20:01:48

标签: ios swift

仅当文本字段不为空或不相同时,我想显示新的ViewController。我总是单击按钮将用户导航到下一个视图,即使不满足条件也要这样做。

@IBAction func nextButtonPressed(_ sender: Any) {
    self.performSegue(withIdentifier: "goToLastRegisterView", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if usernameTextField.text == "" || passwordTextField.text == "" || confirmedPasswordTextField.text == "" {
        errorLabel.isHidden = false
        errorLabel.text = "All fields are required"
    }
    else{
        if passwordTextField.text == confirmedPasswordTextField.text{
            if segue.identifier == "goToLastRegisterView"{
                let secondRegisterVC = segue.destination as! RegisterSecondViewController
                secondRegisterVC.userName = usernameTextField.text!
                secondRegisterVC.password = passwordTextField.text!

            }
        }
        else{
            errorLabel.isHidden = false
            errorLabel.text = "Different passwords"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您进行了self.performSegue(withIdentifier:操作,现在要检查override func prepare(for内的文本副本为时已晚,则必须通过按钮操作进行操作

@IBAction func nextButtonPressed(_ sender: Any) {

    if usernameTextField.text == "" || passwordTextField.text == "" || confirmedPasswordTextField.text == "" {
        errorLabel.isHidden = false
        errorLabel.text = "All fields are required"
        return 
    }
    self.performSegue(withIdentifier: "goToLastRegisterView", sender: self)
}