shouldPerformSegueWithIdentifier

shouldPerformSegueWithIdentifier

我有一个检查登录是否正确的功能。如果可以,我将显示下一个屏幕。如果用户不正确,我将取消segue并显示警报。

出现问题是因为当我在shouldPerformSegueWithIdentifier函数中调用此函数时,我将值赋给了布尔变量(如果用户正确与否),然后ShouldPerformSegueWithIdentifier的返回值就是该布尔值。问题在于它不采用该值,而是保留默认值。这是我的代码:

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

}

提前致谢。

最佳答案

只需将segue与ViewController A附加到ViewController B,假设segue的名称是mySegue1,
假设单击了登录按钮。检查一切是否正确。如果正确的话

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

09-07 11:44