[英]How could I inject dependency with custom initializer in UITabBarController?
I have a custom UITabBarController without any NIB file. 我有一个没有任何NIB文件的自定义UITabBarController。 The navigation controllers of all modules are created in the viewDidLoad method with creator function. 所有模块的导航控制器都在viewDidLoad方法中使用creator函数创建。 I want to add the coreDataStack with a param via those creator function like this: 我想通过这样的创建函数添加coreDataStack和param:
override func viewDidLoad() {
super.viewDidLoad()
let aModuleNavCont = aModuleBuilder.createaModule(coreDataStack) as! UINavigationController
...
}
But before this I have to set the coreDataStack property in my own UITabBarController class. 但在此之前,我必须在我自己的UITabBarController类中设置coreDataStack属性。 I've wrote this init: 我写了这个init:
init(coreDataStack: CoreDataStack) {
self.coreDataStack = coreDataStack
super.init(nibName: nil, bundle: nil)
}
But it said: 'required' initializer 'init(coder:)' must be provided by subclass of 'UITabBarController' Therefore I generated this one with Xcode: 但它说:'required'initulator'init(coder :)'必须由'UITabBarController'的子类提供。因此我使用Xcode生成了这个:
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Now the build is succeeded, and the app is run well (it looks like well), but I have a hunch it is not soo good solution. 现在构建成功,应用程序运行良好(看起来很好),但我有预感它不是太好的解决方案。
I' tried add super.init(coder: aDecoder)
line in the required init, but it said "Property 'self.coreDataStack' not initialized at super.init call". 我尝试在所需的init中添加super.init(coder: aDecoder)
行,但它说“属性'self.coreDataStack'未在super.init调用时初始化”。
I've tried several things, but I have no idea, what should I do to handle this well. 我尝试了几件事,但我不知道,我该怎么做才能很好地处理好这件事。 Could you help with this? 你能帮忙吗?
Thank you in advance for any help you can provide. 提前感谢您提供的任何帮助。
You are right to be nervous about this, but it is actually correct and the standard way to implement the Coder init when you are not using a NIB. 你对此感到紧张是正确的,但它实际上是正确的,并且是你在不使用NIB时实现Coder init的标准方法。
This barnacle (weird thing that feels glued onto the system) comes from the interaction between Cocoa's highly dynamic nature and Swift's more strict initialization requirements. 这个藤壶(感觉粘在系统上的奇怪之处)来自Cocoa的高度动态性和Swift更严格的初始化要求之间的相互作用。 (Swift added these initialization requirements specifically because of the lessons we learned from ObjC ....) (Swift特别添加了这些初始化要求,因为我们从ObjC中学到了很多东西 ....)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.