我正在尝试重构应用程序以对核心数据堆栈使用依赖项注入(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/