Search code examples
iosswiftsegue

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


I wanted to save all data from textfields to firebase but after pressing the sign up button it proceeds even if all 3 text fields are empty

import UIKit import Firebase

class SignUpViewController: UIViewController {

Here are all outlets from storyboard

@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 {

/* Here is must perform segue only after all 3 textfields are filled with data and there are no errors */

                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.
}
*/

}


Solution

  • I had the same problem, if you dragged the segue from the sign up button directly to the other view controller (by holding control and dragging), it will perform the segue even if it does not match the requirements once the button is clicked. If thats the case, then delete the segue and make a new one, drag the segue from the View controller icon to the other view controller using the same identifier.