如何将依赖项注入到iOS视图控制器中? [英] How do I inject dependencies into an iOS view controller?

查看:48
本文介绍了如何将依赖项注入到iOS视图控制器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图控制器需要将消息发送到几个模型对象.如何在视图控制器中获取对这些模型对象的引用?

My view controllers need to send messages to a couple of model objects. How do I obtain references to these model objects inside the view controller?

这些模型对象是单个"(因为在系统中一次只能有一个副本),并且它们由多个视图控制器使用.因此,我无法在每个视图控制器的init方法中实例化它们.

These model objects are "singletons" (in that there should only be one copy of them in the system at once) and they are used by multiple view controllers. So I can't instantiate them in the init method of each view controller.

我不能使用构造函数注入,因为运行时会选择用于创建视图控制器的init方法.

I can't use constructor injection as the runtime chooses the init method that gets used to create the view controller.

在任何时候(我都知道),我都无法使用"setter注入"功能.我既没有对新构造的视图控制器的引用,也没有对单人"模型对象的引用.

I can't use "setter injection" as at no point (that I am aware of) do I have both a reference to the newly constructed view controller and references to the "singleton" model objects.

我不希望将模型对象转换为适当的单例,并从视图控制器中对其调用静态方法以检索单例实例,因为这是可测试性的问题. (在AppDelegate上将模型对象作为属性与执行此操作基本相同.)

I don't want to turn the model objects into proper singletons and call a static method on them from the view controllers to retrieve the singleton instance, as this is a problem for testability. (Having the model objects as properties on the AppDelegate is essentially the same as doing this.)

我正在将iOS 6与Storyboards一起使用.

I am using iOS 6 with Storyboards.

推荐答案

我刚刚处理了同样的问题.由于我使用情节提要板,因此我没有实例化我的UIViewControllers,因此不能使用构造函数注入".我必须使用二传手注入辞职.

I've just dealt with the same problem. Since I'm using storyboards I don't instantiate my UIViewControllers, so I can't use "constructor injection". I must resign myself using setter injection.

我的应用程序根目录是UITabViewController.假设它有两个UINavigationController,第一个为AControllerView,第二个为BControllerView.在AppDelegate.applicationDidFinishLaunchingWithOptions中,您可以通过以下方式检索根控制器:

My app root is a UITabViewController. Let's say it has two UINavigationControllers, having the first a AControllerView and the second BControllerView. In AppDelegate.applicationDidFinishLaunchingWithOptions you can retrieve the root controller this way:

UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;

UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;

然后您可以遍历控制器:

Then you can iterate through the controllers:

NSArray* viewControllers = [tabBarController viewControllers];
for (UIViewController *viewController in viewControllers) {
    UINavigationController *navigationController = (UINavigationController*) viewController;
    UIViewController *viewController = navigationController.topViewController;
    if ([viewController isKindOfClass: [AControllerView class]]) {
        AControllerView *a = (AControllerView*) viewController;
        // Inject your stuff
    }
    if ([viewController isKindOfClass: [BControllerView class]]) {
        BControllerView *b = (BControllerView*) viewController;
        // Inject your stuff
    }
}

希望它会有所帮助.

这篇关于如何将依赖项注入到iOS视图控制器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆