简体   繁体   English

我如何在UITabBarController中为自定义初始化程序注入依赖?

[英]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.

相关问题 如何显示与附加到UITabBarControLler的ViewController不同的ViewController - How could I display different ViewController from ViewController attached to UITabBarControLler 如何在 UITableViewCell 上使用自定义初始值设定项? - How can I use a custom initializer on a UITableViewCell? 如何将自定义图像添加到UITabBarController - How to add custom image to UITabBarController 如何为自定义UITableViewCell类设置初始化程序? - How do I set an initializer for a custom UITableViewCell class? 如何从UITabBarController展开到自定义视图 - How to unwind from UITabBarController to custom view 故事板-如何在UINavigationController中使用UITabbarController自定义UITabbarItem - Storyboard - How to custom UITabbarItem with UITabbarController inside UINavigationController 如何在UITabBarController中的选项卡上添加自定义手势 - How to add custom gesture to a tab in UITabBarController 如何在不使用 IB 的情况下使用自定义 UITabBar 类创建 UITabBarController? - How do I create a UITabBarController with a custom UITabBar class without using IB? 我可以使用TabBarItem上的自定义事件点击自定义UITabBarController吗? - Can i make custom UITabBarController with custom event tap on TabBarItem? 在应用程序中注入依赖项后,如何防止 ViewController 被释放:DidFinishLaunchingWithOptions: - How can I prevent ViewControllers from being deallocated after I inject them with a dependency in application:DidFinishLaunchingWithOptions:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM