performSegueWithIdentifier没有发起诅咒

时间:2015-09-21 22:39:54

标签: ios swift segue uistoryboard uistoryboardsegue

我的performSegueWithIdentifier方法的实现不是启动segue。

我尝试通过在方法之前和之后放置print语句来进行调试:它在之前打印,而不是之后打印,这显然表明该方法不起作用,对吧?

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)

                    }

                })
            }

        }

    }

1 个答案:

答案 0 :(得分:3)

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

在主线程上执行你的segue:

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