为什么即使不满足包装条件,segue也会执行?

时间:2016-04-10 10:57:06

标签: ios swift

我想在用户登录成功时转到不同的视图。

@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)
    }
}

segue位于else块中。 if块检查用户登录时是否有错误,如果有错误则打印错误。传入错误的ID时会出现错误,但是,应用程序仍然会分段。

为什么会发生这种情况,我该如何预防呢!

感谢?

1 个答案:

答案 0 :(得分:0)

修复:问题是我的segue位于按钮和ViewController之间,而不是2个ViewControllers之间。

在两个ViewController之间重新创建segue解决了这个问题。

  • 致Arun Gupta和Rahul Katariya的信用