Cancel a segue in swift with shouldPerformSegueWithIdentifier

3.8k views Asked by At

I have a function that checks if the log in is correct. If it's OK, I show next screen. If the user is incorrect, I cancel the segue and show an alert .

The problem arises because when I call this function in the shouldPerformSegueWithIdentifier function, I give value to a Boolean variable (if the user is correct or not) and then the return value of shouldPerformSegueWithIdentifier is this Boolean. The problem is that it does not take that value and remains in the default. This is my code:

override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {

    var userIsCorrect = false //THIS IS THE BOOLEAN

    if identifier == "fromLogInToGetIn" {

        self.loginRequest("http://myurl.com",

            withParams: ["email":"[email protected]","password":"password"])

        {

            (succeeded: Bool, msg: String) -> () in

            var alert = UIAlertView(title: "Success!", message: msg, delegate: nil, cancelButtonTitle: "Okay.")

            if(succeeded) {

                if msg == "0"

                {

                    userIsCorrect = false // BOOLEAN DOES NOT TAKE THE VALUE

                    alert.title = "Error"

                    alert.message = "Incorrect user"

                }

                else

                {

                    userIsCorrect = true // BOOLEAN DOES NOT TAKE THE VALUE

                }

            }

            else {

                userIsCorrect = false // BOOLEAN DOES NOT TAKE THE VALUE

                alert.title = "Error"

                alert.message = "Something is wrong"

            }

            // Move to the UI thread

            dispatch_async(dispatch_get_main_queue(), { () -> Void in

                // Show the alert

                if userIsCorrect == false

                {

                    alert.show()

                }

            })

        }

    }

    return userIsCorrect // ALWAYS RETURN DEFAULT VALUE

}

Thanks in advance.

1

There are 1 answers

0
Sheshnath On BEST ANSWER

just attache segue with, ViewController A to ViewController B, suppose name of segue is mySegue1, suppose Login Button clicked. check everything is correct or not. if it's correct then,

         self.performSegueWithIdentifier("mySegue1", sender: self)