【问题标题】:performSegueWithIdentifier Not Initiating a SegueperformSegueWithIdentifier 不启动 Segue
【发布时间】:2015-12-18 18:25:36
【问题描述】:

我对 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)

                    }

                })
            }

        }

    }

【问题讨论】:

  • 在 Xcode 中运行程序的调试版本并观察调试控制台。我的猜测是该调用引发了导致您的函数终止的异常。
  • @DuncanC 你是怎么做到的?这就是我一直在寻找的
  • 从视图菜单中,选择“调试区域>激活控制台”。然后在项目窗口的右下角查看 2 个小蓝色框。确保它们都是蓝色的,并将调试控制台的两个部分之间的分隔线移动到两个部分之间的一半左右。然后运行您的程序并触发相关代码。

标签: ios swift segue uistoryboard uistoryboardsegue


【解决方案1】:

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

在主线程上执行你的 segue:

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

【讨论】:

  • 该死的你给我禁食 30 秒 ;)
  • 这是 iOS 9 的新功能吗?
  • 好收获。我错过了。对于 OP,您正在使用一个名为 PFUser.logInWithUsernameInBackground 的解析函数。该方法名称告诉您它在后台执行此操作。在任何版本的 iOS 下,您都无法在后台更改 UI。
  • 对于 OP,如果您询问 dispatch_async 是否是 iOS 9 的新手,则不是。如果没记错的话,它是在 iOS 4 中添加的。
  • 现在我收到了Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: xxxxxxxxx>) doesn't contain a view controller with identifier 'UINavigationController-xxx-xx-xxx''
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多