Yunfei Lu
Yunfei Lu

Reputation: 429

How to logout and segue to the viewcontroller correctly?

My idea is creating a log in app, when I log in I will segue to a new view controller and do something, and my code is like that:

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


                }
            }


        }


    }


}

I put sign-up and log-in into the same button, and I use signUpActive to take them apart, and I put self.performSegueWithIdentifier("push", sender: nil) in the log-in part, so when I input the correct password and username I can segue to a new view controller.

It works well. But when I add a new button in that new view controller which is log-out, when I click that button, I will segue to the first view controller and I use PFuser.logout() to logout, and I need to re-input the correct username and password.

Then here comes a question, even if I input the wrong username, it will segue to the second view controller, although it will show an alert saying that the username is not valid. Meanwhile, even if I click sign up not log in, it will segue too. why does it happen? Can anyone help me?

Upvotes: 1

Views: 444

Answers (1)

Yunfei Lu
Yunfei Lu

Reputation: 429

after trying one day, I found the answer, by using unwinded segue, this problem can be easily fixed.

Upvotes: 3

Related Questions