Reputation: 146
I'm trying to perform a segue from my AppDelegate
. I know I can't actually perform a segue from the AppDelegate
, since a segue is a transition from one scene to another, so I instantiate the two ViewControllers
concerned in my 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
Then later, I try to loadingViewController.performSegueWithIdentifier("segueToLogin", sender: self)
where segueToLogin
is a custom segue identifier in my storyboard
The error I get (which doesn't crash the app) is
Tacklebox[27258:1016738] Warning: Attempt to present <Tacklebox.LoginViewController: 0x7fd8a9646bd0> on <Tacklebox.LoadingViewController: 0x7fd8a943d940> whose view is not in the window hierarchy!
and the end result is I stay on the sourceViewController
, loadingViewController
.
Upvotes: 1
Views: 1609
Reputation: 37300
The problem is that you've created these view controllers in the App Delegate, but since you haven't used those particular instances to navigate between views, those view controller instances aren't the same view controllers actually on the stack; thus the error saying the LoadingViewController
instance you're trying to segue to "is not in the window hierarchy."
You're going to have to get the instance of the active LoadingViewController
. Since that view controller from which you're performing a segue is bound to be (i.e. supposed to be) visible and since (as indicated by your Storyboard), the rootViewController
is a UINavigationController
, try this to access the current LoadingViewController
in the navigation hierarchy:
let navigationController = self.window?.rootViewController as UINavigationController
let loadingViewController = navigationController.visibleViewController as LoadingViewController
loadingViewController.performSegueWithIdentifier("segueToLogin", sender: self)
Upvotes: 3