【问题标题】:Initializing UINavigationController with Coordinator Pattern and Storyboard使用 Coordinator Pattern 和 Storyboard 初始化 UINavigationController
【发布时间】:2019-09-07 18:12:47
【问题描述】:

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

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

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

【问题讨论】:

    标签: swift xcode storyboard


    【解决方案1】:

    我刚试过这个,它有效。你在做这样的事情吗?

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

    【讨论】:

    • 嗯,这意味着导航控制器可以像视图控制器一样实例化并转换为 UINavigationController?真的这么简单吗?
    • 它必须是情节提要中的导航控制器才能使向下转换工作。
    猜你喜欢
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    相关资源
    最近更新 更多