台风没有注入财产(没有故事板) [英] Typhoon not injecting property (without storyboard)

查看:113
本文介绍了台风没有注入财产(没有故事板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用带有 initWithNibName的XIB将视图注入到视图控制器中:bundle:

示例:

这是我的汇编:

@implementation AppAssembly

- (ViewControllerC *)viewControllerC
{
    return [TyphoonDefinition withClass:[ViewControllerC class]
                          configuration:^(TyphoonDefinition *definition) {
      [definition injectProperty:@selector(name) with:@"Injected string"];
    }];
}

@end

ViewControllerA代码:

ViewControllerA code:

@implementation ViewControllerA

- (IBAction)buttonAction:(id)sender 
{
    ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:nil];
    [self.navigationController pushViewController:viewControllerB animated:YES];
}

@end

ViewControllerB代码:

ViewControllerB code:

@implementation ViewControllerB

- (IBAction)buttonAction:(id)sender 
{
    ViewControllerC *viewControllerC = [[ViewControllerC alloc] initWithNibName:@"ViewControllerC" bundle:nil];
    [self.navigationController pushViewController:viewControllerC animated:YES];
}

@end

ViewControllerC代码:

ViewControllerC code:

@interface ViewControllerC : UIViewController

@property (copy, nonatomic) NSString *name;

@end

@implementation ViewControllerC

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // Why is this not being injected?
    self.title = self.name;
}

@end

AppDelegate代码:

AppDelegate code:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ViewControllerA *rootViewController = [[ViewControllerA alloc] initWithNibName:@"ViewControllerA" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];

    return YES;
}

@end

我已经检查了样品,他们与这种方法不同。请问你能告诉我我做错了什么?

I have already checked the samples, and they are different to this approach. Please can you tell me what am I doing wrong?

谢谢。

推荐答案

Typhoon是一个依赖注入容器,这意味着它不会对你的课程进行调整,调配或其他方式。因此,为了获得注入了依赖项的类的实例,我们必须询问Typhoon。

Typhoon is a dependency injection container, meaning it doesn't instrument, swizzle or otherwise touch your classes. Therefore to get an instance of a class with dependencies injected, we have to ask Typhoon.

为什么不用于Storyboard?

当使用plist集成时,Typhoon注册TyphoonStoryboard代替UIStoryboard,根据故事板中的定义发出实例 - 就像普通的故事板一样,具有额外的好处注入依赖项。

When using plist integration, Typhoon registers TyphoonStoryboard in place of UIStoryboard to emit instances based on definitions in the storyboard - works just like a normal storyboard with the added benefit that dependencies are injected.

在其他地方,为了获取注入了依赖项的实例,我们使用汇编接口,并询问Typhoon。

In other places, to get instances with dependencies injected, we use the assembly interface, and ask Typhoon.

解决方案步骤:

在装配中添加ViewControllerB的定义。 (在Objective-C中,如果您愿意,也可以使用自动注入宏)。

Add a definition for ViewControllerB in your assembly. (In Objective-C you can also use auto-injection macros if you wish).

- (ViewControllerB *)viewControllerB
{
    return [TyphoonDefinition withClass:[ViewControllerB class] 
        configuration:^(TyphoonDefinition *definition) {

        [definition useInitializer:@selector(initWithNibName:bundle:) 
            parameters:^(TyphoonMethod *initializer) {

            [initializer injectParameterWith:@"ViewControllerB"];
            [initializer injectParameterWith:[NSBundle mainBundle]];
        }];

        [definition injectProperty:@selector(assembly) with:self];
    }];
}

向ViewControllerB添加属性:

//Start with this, next task is to back this with a protocol
@property(nonatomic, strong) AppAssembly *assembly;

现在更改实例化ViewControllerC的按钮操作:

ViewControllerC *viewControllerC = self.assembly.viewControllerC
[self.navigationController pushViewController:viewControllerC animated:YES];

现在我的应用程序类依赖于我的TyphoonAssembly,如果我不想要这个怎么办?

所有Typhoon程序集都可以由协议支持,因此您的应用程序只能看到实例提供程序,而不是Typhoon。如果您曾希望从Typhoon迁移,只需提供该协议的替代实现 - 您仍将拥有强大的架构。

All Typhoon assemblies can be backed by a protocol, so that your application sees only a provider of instances, and not Typhoon. If you ever wished to migrate from Typhoon, simply provide an alternative implementation of that protocol - you'll still have a robust architecture in place.

这篇关于台风没有注入财产(没有故事板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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