performSegueWithIdentifier 未启动 Segue

performSegueWithIdentifier Not Initiating a Segue

我执行的 performSegueWithIdentifier 方法没有启动 segue。

我试过通过在方法前后放置打印语句来调试:它打印之前,但不是之后,这显然表明该方法不起作用,对吗?

class ViewController: UIViewController{

    var signupActive = true

    @IBOutlet weak var username: UITextField!
    @IBOutlet weak var password: UITextField!

    var errorMessage = "Please try again later"

    func displayAlert(title: String, message: String) {

        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction((UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
            self.dismissViewControllerAnimated(true, completion: nil)
        })))

        self.presentViewController(alert, animated: true, completion: nil)

    }

    @IBAction func signUp(sender: AnyObject) {

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

            displayAlert("Error in form", message: "Please enter a username and password")

        } else {

            UIApplication.sharedApplication().beginIgnoringInteractionEvents()

            var errorMessage = "Please try again later"

            if signupActive == true {

                var user = PFUser()
                user.username = username.text
                user.password = password.text
                user.signUpInBackgroundWithBlock({ (success, error) -> Void in

                    UIApplication.sharedApplication().endIgnoringInteractionEvents()

                    if error == nil {

                        // Right here and below as well

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


                    } else {

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

                            errorMessage = errorString

                        }

                        self.displayAlert("Failed signup", message: errorMessage)

                    }
                })

            } else {

                PFUser.logInWithUsernameInBackground(username.text!, password: password.text!, block: { (user, error) -> Void in

                    UIApplication.sharedApplication().endIgnoringInteractionEvents()

                    if user != nil {

                        // Here too

                        print("kay")
                        self.performSegueWithIdentifier("login", sender: self)
                        print("yay")


                    } else {

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

                            errorMessage = errorString
                        }

                        self.displayAlert("Failed login", message: errorMessage)

                    }

                })
            }

        }

    }

您需要在主线程上执行 segue,因为当前您正在一个块中执行 segue。

要在主线程上执行您的 segue:

dispatch_async(dispatch_get_main_queue()){
            self.performSegueWithIdentifier("login", sender: self)
}