使用协调器模式和 Storyboard 初始化 UINavigationController

Initializing UINavigationController with Coordinator Pattern and Storyboard

我目前正在将我的应用程序从 MVC 迁移到 MVVM。另外我想使用 Coordinator 模式。由于我在我的应用程序中使用故事板,所以我在这里遇到了问题。

我成功地从情节提要中实现了视图控制器的实例化。但是,当我在 Coordinator 中初始化导航控制器 UINavigationController() 时,应用程序 - 当然 - 不使用我在情节提要中设计的导航控制器的设计。

有没有一种方法可以从情节提要中初始化导航控制器,类似于视图控制器storyboard.inistantiateViewController(withIdentifier)

我刚刚试过了,效果不错。你在做这样的事吗?

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")
}