【问题标题】:Passing a Dependency to Tab Bar View Controllers将依赖项传递给选项卡栏视图控制器
【发布时间】:2018-07-04 01:28:48
【问题描述】:

我正在尝试重构一个应用程序,以便为核心数据堆栈使用依赖注入。

这里有一个很好的解释: http://cleanswifter.com/dependency-injection-with-storyboards/

我的问题是,在我的应用程序中,由 uittabbarcontroller 链接的视图控制器嵌入在导航控制器中,所以这段代码:

  if let tab = window?.rootViewController as? UITabBarController {
    for child in tab.viewControllers ?? [] {
      if let top = child as? PersistenceStackClient {
        top.set(stack: persistenceStack)
      }
    }
  }

从未见过他们。 我的直觉是,我需要将 for child in 更改为引用导航控制器子级的内容,但所有尝试都失败了。

我想我需要一些类似的东西:

for child in tab.navigationController?.viewControllers

但这似乎没有任何作用。

【问题讨论】:

    标签: swift core-data uitabbarcontroller


    【解决方案1】:

    认为我已经解决了这个问题:

        if let tab = window?.rootViewController as? UITabBarController {
            for child in tab.viewControllers ?? [] {
                for nav in child.childViewControllers {
                    if let top = nav as? PersistenceStackClient {
                        top.setStack(stack: coreDataStack)
                    }
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-08
      • 2016-03-27
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 2012-05-26
      相关资源
      最近更新 更多