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

例:

这是我的程序集:

@implementation AppAssembly

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

@end

ViewControllerA代码:
@implementation ViewControllerA

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

@end

ViewControllerB代码:
@implementation ViewControllerB

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

@end

ViewControllerC代码:
@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代码:
@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

我已经检查了样本,它们与这种方法不同。请您告诉我我做错了什么吗?

谢谢。

最佳答案

台风是一个依赖项注入容器,这意味着它不会检测,搅动或以其他方式触摸您的 class 。因此,要获得注入依赖项的类的实例,我们必须询问Typhoon。

为什么不使用Storyboard?

当使用plist集成时,Typhoon会注册TyphoonStoryboard代替UIStoryboard来根据情节提要中的定义发出实例-就像普通情节提要一样工作,具有注入依赖项的附加好处。

在其他地方,要获取注入依赖项的实例,我们使用组装接口,并询问Typhoon。

解决方案步骤:

在程序集中添加ViewControllerB的定义。 (如果需要,您还可以在Objective-C中使用自动注入宏)。

- (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迁移,只需提供该协议的替代实现-您仍将拥有一个强大的体系结构。

关于ios - 台风未注入(inject)属性(property)(无 Storyboard ),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31507439/

10-14 21:32