通过按下注册按钮,即使所有文本字段都是空的,segue 也会执行

By pressing sign up button segue performs even if all text fields are empty

我想将文本字段中的所有数据保存到 firebase,但在按下注册按钮后,即使所有 3 个文本字段都是空的,它也会继续

导入 UIKit 导入 Firebase

class SignUpViewController: UIViewController {

这是故事板中的所有渠道

@IBOutlet weak var userNameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var signUpButton: UIButton!
@IBOutlet weak var errorLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    userNameTextField.backgroundColor = .clear
    userNameTextField.layer.cornerRadius = 27
    userNameTextField.layer.borderWidth = 1
    userNameTextField.layer.borderColor = UIColor.systemGreen.cgColor
    
    emailTextField.backgroundColor = .clear
    emailTextField.layer.cornerRadius = 27
    emailTextField.layer.borderWidth = 1
    emailTextField.layer.borderColor = UIColor.systemGreen.cgColor
    
    passwordTextField.backgroundColor = .clear
    passwordTextField.layer.cornerRadius = 27
    passwordTextField.layer.borderWidth = 1
    passwordTextField.layer.borderColor = UIColor.systemGreen.cgColor
}

@IBAction func signUpPressed(_ sender: UIButton) {
   
    if let email = emailTextField.text, let password = passwordTextField.text {
        Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
            if let e = error {
                self.errorLabel.text = e.localizedDescription
            } else {

/* 只有在所有 3 个文本字段都填充了数据并且没有错误之后才必须执行 segue */

                self.performSegue(withIdentifier: "SignUpToMap", sender: self)
                //Navigate to the ChatViewController
                let db = Firestore.firestore()
                
                db.collection("users").addDocument(data: ["username": self.userNameTextField.text!, "uid": authResult!.user.uid]) { (error) in
                    if let e = error {
                        self.errorLabel.text = e.localizedDescription
                    }
                }
                
            }
        }
    }
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/

}

我有同样的问题,如果你直接从注册按钮拖拽segue到另一个view controller(按住control并拖拽),它会执行segue,即使它不符合要求一旦按钮被点击。如果是这样,则删除 segue 并创建一个新的,将 segue 从 View controller 图标拖到另一个使用相同标识符的 view controller。