简体   繁体   中英

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.

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: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM