swift - 将依赖项传递给选项卡栏 View Controller

标签 swift core-data uitabbarcontroller

我正在尝试重构应用程序以对核心数据堆栈使用依赖项注入(inject)。

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

我的问题是,在我的应用程序中,由 uittabbarcontroller 链接的 View Controller 嵌入在导航 Controller 中,因此以下代码:

  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 更改为引用导航 Controller 子级的内容,但所有尝试都失败了。

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

for child in tab.navigationController?.viewControllers

但这似乎没有任何作用。

最佳答案

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

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

关于swift - 将依赖项传递给选项卡栏 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51159251/

相关文章:

ios - 在移动到 iOS 中的下一个选项卡之前显示警报?

ios - 带乘数的前导约束不起作用

swift - 为什么缓存已删除的 tableView 部分的属性?

swift - 以更优雅的方式从字典中获取值

core-data - 使用谓词在结果中搜索

ios - UITabBarController - 如何切换到特定类型的选项卡

ios - CoreBluetooth 在应用程序终止时建立外设连接

swift - 迭代 NSSet - SwiftUI 2.0

objective-c - 如何在核心数据中保留有序列表

ios - 在标签栏顶部显示 View