来自app delegate的自定义segue

时间:2014-12-20 16:11:13

标签: ios swift uiviewcontroller uistoryboard

我正在尝试从我的AppDelegate执行一个segue。我知道我实际上无法从AppDelegate执行segue,因为segue是从一个场景到另一个场景的过渡,所以我在ViewControllers中实例化了两个AppDelegate

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loadingViewController = storyboard.instantiateViewControllerWithIdentifier("loadingViewController") as UIViewController
let loginViewController = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as UIViewController
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("mainViewController") as UIViewController

然后,我尝试loadingViewController.performSegueWithIdentifier("segueToLogin", sender: self)其中segueToLogin是我的故事板中的自定义segue标识符

enter image description here

我得到的错误(不会导致应用程序崩溃)是

Tacklebox[27258:1016738] Warning: Attempt to present <Tacklebox.LoginViewController: 0x7fd8a9646bd0> on <Tacklebox.LoadingViewController: 0x7fd8a943d940> whose view is not in the window hierarchy!

最后的结果是我留在sourceViewControllerloadingViewController

1 个答案:

答案 0 :(得分:3)

问题是您已在App Delegate中创建了这些视图控制器,但由于您还没有使用这些特定实例在视图之间导航,因此这些视图控制器实例不是相同的视图控制器实际上在堆栈上;因此,错误说明您试图转向&#34;的LoadingViewController实例不在窗口层次结构中。&#34;

您必须获取有效LoadingViewController的实例。由于您从中执行segue的视图控制器必然(即应该是)可见,并且由于(如故事板所示),rootViewControllerUINavigationController,请尝试这样可以访问导航层次结构中的当前LoadingViewController

let navigationController = self.window?.rootViewController as UINavigationController
let loadingViewController = navigationController.visibleViewController as LoadingViewController
loadingViewController.performSegueWithIdentifier("segueToLogin", sender: self)