user3405152 user3405152 - 6 months ago 134
iOS Question

Perform Segue from App Delegate swift

I need to launch a view controller from the app delegate.

In the way you would perform a segue between view controllers.

I have an if statement that if true needs to show a view controller, this is in the app delegate.

How do I do this from the app delegate?


c_rath's answer is mostly right, but you don't need to make the view controller a root view controller. You can in fact trigger a segue between the top view on the navigation stack and any other view controller, even from the App Delegate. For example, to push a storyboard view controller, you could do this:

// Access the storyboard and fetch an instance of the view controller
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var viewController: MainViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as MainViewController

// Then push that view controller onto the navigation stack
var rootViewController = self.window!.rootViewController as UINavigationController
rootViewController.pushViewController(viewController, animated: true)