Typhoon not injecting property (without storyboard)

前端 未结 2 1737
慢半拍i
慢半拍i 2021-01-20 05:44

I cannot get properties injected into view controllers using XIBs with initWithNibName:bundle:

Example:

This is my assembly:

@im         


        
2条回答
  •  滥情空心
    2021-01-20 06:41

    So, I had to first ask my AppAssembly for an instance of viewControllerB with the assembly injected and then I was able to use it to get an instance of viewControllerC.

    AppAssembly code:

    - (ViewControllerB *)viewControllerB
    {
        return [TyphoonDefinition withClass:[ViewControllerB class]
                              configuration:^(TyphoonDefinition *definition) {
                                  [definition useInitializer:@selector(initWithNibName:bundle:)
                                                  parameters:^(TyphoonMethod *initializer) {
                                                      [initializer injectParameterWith:@"ViewControllerB"];
                                                      [initializer injectParameterWith:nil];
                                                  }];
    
                                  [definition injectProperty:@selector(assembly) with:self];
                              }];
    }
    
    - (ViewControllerC *)viewControllerC
    {
        return [TyphoonDefinition withClass:[ViewControllerC class]
                              configuration:^(TyphoonDefinition *definition) {
                                  [definition useInitializer:@selector(initWithNibName:bundle:)
                                                  parameters:^(TyphoonMethod *initializer) {
                                                      [initializer injectParameterWith:@"ViewControllerC"];
                                                      [initializer injectParameterWith:nil];
                                                  }];
    
                                  [definition injectProperty:@selector(name) with:@"Injected string"];
        }];
    }
    

    ViewControllerA code:

    @implementation ViewControllerA
    
    - (IBAction)buttonAction:(id)sender
    {
        ViewControllerB *viewControllerB = [[[AppAssembly new] activate] viewControllerB];
        [self.navigationController pushViewController:viewControllerB animated:YES];
    }
    
    @end
    

    ViewControllerB code:

    @implementation ViewControllerB
    
    - (IBAction)buttonAction:(id)sender 
    {
        ViewControllerC *viewControllerC = [self.assembly viewControllerC];
        [self.navigationController pushViewController:viewControllerC animated:YES];
    }
    
    @end
    

    ViewControllerC code:

    @implementation ViewControllerC
    
    - (void)viewDidLoad 
    {
        [super viewDidLoad];
        self.title = self.name;
    }
    
    @end
    

    As you can see I had to do this: ViewControllerB *viewControllerB = [[[AppAssembly new] activate] viewControllerB]; Not sure if there is another way. Because of doing that I was able to have the assembly injected in ViewControllerB, and that let me do this: ViewControllerC *viewControllerC = [self.assembly viewControllerC]; However, I noticed that I could also do ViewControllerC *viewControllerC = [[[AppAssembly new] activate] viewControllerC];, so not sure which the better approach is. Anyway, I think I had to call new and activate at least once.

    Thanks.

提交回复
热议问题