Search code examples
swiftxcodestoryboard

Initializing UINavigationController with Coordinator Pattern and Storyboard


I am currently working on the migration of my app from MVC to MVVM. In addition I would like to use the Coordinator pattern. Since I use storyboards in my apps, I encountered a problem here.

I successfully implemented the instantiation of view controllers from the storyboard. But when I initialize the navigation controller UINavigationController() in the Coordinator, the app - of course - doesn't use the design of the navigation controller as I designed it in the storyboard.

Is there a way to initialize a Navigation Controller from the Storyboard similar to the View Controller storyboard.inistantiateViewController(withIdentifier)?


Solution

  • I just tried this an it works. Are you doing something like this?

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    if let nc = storyboard.instantiateInitialViewController() as? UINavigationController {
        print("got the nav controller")
    }
    // or if it's not the initial, you have to set the id in the storyboard
    if let nc = storyboard.instantiateViewController(withIdentifier: "Nav") as? UINavigationController {
        print("got the nav controller")
    }