简体   繁体   中英

Cancel a segue in swift with shouldPerformSegueWithIdentifier

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@email.com","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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM