如何正确注销并转至 viewcontroller?

How to logout and segue to the viewcontroller correctly?

我的想法是创建一个登录应用程序,当我登录时我会转到一个新的视图控制器并做一些事情,我的代码是这样的:

 @IBAction func signUp(sender: AnyObject) {
    var error = ""

    if username.text == "" || password.text == "" {

        error = "Please enter a username and password"

    }
    if error != "" {
        displayAlert("Error in form", error: error)

    } else {



        activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))     //waiting animation
        activityIndicator.center = self.view.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        view.addSubview(activityIndicator)
        activityIndicator.startAnimating()
        UIApplication.sharedApplication().beginIgnoringInteractionEvents()


        if signupActive == true {                   

            var user = PFUser()
            user.username = username.text
            user.password = password.text




            user.signUpInBackgroundWithBlock {
                (succeeded: Bool, signupError: NSError?) -> Void in

                self.activityIndicator.stopAnimating()

                UIApplication.sharedApplication().endIgnoringInteractionEvents()


                if signupError == nil {
                    println("signed up")


                } else {
                    if let errorString = signupError!.userInfo?["error"] as? NSString
                    {

                        error = errorString as! String

                    } else {

                        error = "Please try again later"

                    }

                    self.displayAlert("Could not sign up", error: error)
                }
            }


        } else {

            PFUser.logInWithUsernameInBackground(username.text as String!, password:password.text as String!) {
                (user: PFUser?, signupError: NSError?) -> Void in


                self.activityIndicator.stopAnimating()
                UIApplication.sharedApplication().endIgnoringInteractionEvents()

                if signupError == nil {

                    println("logged in")

                    self.performSegueWithIdentifier("push", sender: nil)

                } else {

                    if let errorString = signupError!.userInfo?["error"] as? NSString {

                        // Update - added as! String

                        error = errorString as! String

                    } else {

                        error = "Please try again later."

                    }

                    self.displayAlert("Could Not Log In", error: error)


                }
            }


        }


    }


}

我把注册和登录放在同一个按钮里,我用signUpActive把它们分开,我把self.performSegueWithIdentifier("push", sender: nil)放在登录部分,所以当我输入正确的密码和用户名,我可以转到新的视图控制器。

效果很好。但是当我在注销的新视图控制器中添加一个新按钮时,当我单击该按钮时,我将转到第一个视图控制器并使用 PFuser.logout() 注销,我需要重新输入正确的用户名和密码。

那么问题来了,即使我输入了错误的用户名,它也会跳转到第二个视图控制器,尽管它会显示用户名无效的警告。同时,即使我点击注册而不是登录,它也会继续。为什么会这样?谁能帮帮我?

经过一天的尝试,我找到了答案,通过使用unwinded segue,这个问题很容易解决。