Nishant Roy
Nishant Roy

Reputation: 1141

Why is the segue executing even though the wrapping conditional is not met?

I want to segue to a different view when the user login is successful.

@IBAction func loginAction(sender: UIButton) {
    let email = self.email.text
    let password = self.password.text

    if (email != "" && password != "") { //check that password and email are entered
        BASE_REF.authUser(email, password: password, withCompletionBlock: { (error, authData) -> Void in
            if (error != nil) { //check if login was successful
                print(error)
            } else {
                NSUserDefaults.standardUserDefaults().setValue(authData.uid, forKey: "uid")

                print ("Logged in :)")

                self.performSegueWithIdentifier("loginSuccessful", sender: nil)
                self.logoutButton.hidden = false
            }
        })
    } else { //otherwise, return error and show alert
        let alert = UIAlertController(title: "Error", message: "Enter Email and Password", preferredStyle: .Alert)

        let action = UIAlertAction(title: "OK", style: .Default, handler: nil)

        alert.addAction(action)

        self.presentViewController(alert, animated: true, completion: nil)
    }
}

The segue lies in the else block. The if block checks if there was an error in logging the user in and prints an error if there was. The error is printing when an incorrect ID is passed in, however, the app still segues anyway.

Why is this happening and how can I prevent it!

Thanks?

Upvotes: 0

Views: 54

Answers (1)

Nishant Roy
Nishant Roy

Reputation: 1141

FIXED: The problem was that my segue was between the button and the ViewController instead of between 2 ViewControllers.

Recreating the segue between the two ViewControllers solved the problem.

  • Credits to Arun Gupta and Rahul Katariya

Upvotes: 0

Related Questions