Matte.Car
Matte.Car

Reputation: 2029

Load a controller embed to a UINavigationController from AppDelegate in Swift

I need to display from AppDelegate a table view embed to an UINavigationController.

Normally I use

    let viewController: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("prova") as ViewController
    window?.rootViewController?.presentViewController(viewController, animated: false, completion: nil)

but it doesn't work with embed controller, how need to be adapted this code?

Here is a test project.

Upvotes: 2

Views: 3211

Answers (1)

jday
jday

Reputation: 578

You need to instantiate the UINavigationController with its identifier from your storyboard. The navigation controller should be connected to its rootViewController in the storyboard and will automatically show it.

let navController: UINavigationController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("provaNavController") as UINavigationController
window?.rootViewController?.presentViewController(navController, animated: false, completion: nil)

Update

Since you are not setting an initial view controller in the storyboard, use this code:

window = UIWindow(frame: UIScreen.mainScreen().bounds)
let navController: UINavigationController = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("provaNavController") as UINavigationController
window?.rootViewController = navController
window?.makeKeyAndVisible()

You also need to clear out Main.storyboard from the general project settings: enter image description here

Upvotes: 4

Related Questions