Typhoon with Storyboard, instantiating ViewController

佐手、 提交于 2019-12-02 10:38:10

问题


When working with storyboards in Typhoon, if I do something like this in the assembly

- (id)myController
{

    return [TyphoonDefinition withClass:[BigController class] configuration:^(TyphoonDefinition *definition) {
        [definition injectProperty:@selector(dao) with:[_dataAssembly dao]];
    }];
}

Later I want the factory to hand me the controller from the Typhoon story board however I end up with the plain controller created using alloc/init

vc=  [_factory componentForType:[BigController class]];

In AppDelegate I am using the typhoon storyboard as follows

TyphoonComponentFactory *factory = [[TyphoonBlockComponentFactory alloc] initWithAssemblies:@[[Q_Assembly assembly],[P_Assembly assembly]]];

I can go back to using the StoryboardWithIdentifier...but I would like to use the _factory to be able to get the reference to the controller I want from storyboard.


回答1:


Have you tried declaring the storyboard as a factory component? Here's an example:

//Create the factory definition
-(id)myStoryBoard
{
    return [TyphoonDefinition withClass:[TyphoonStoryboard class] configuration:
        ^(TyphoonDefinition* definition) {

         [definition useInitializer:@selector(storyboardWithName:factory:bundle) parameters:
            ^(TyphoonInitializer* initializer) {
            [initializer injectParameterWith:@"storyBoardName"];
            [initializer injectParameterWith:self];
            [initializer injectParameterWith:[NSBundle mainBundle]];
         }
         definition.scope = TyphoonScopeSingleton; //Let's make this a singleton
    }
}

//Create definitions that will be emitted by the factory
-(id)firstVc
{
    return [TyphoonDefinition withFactory:[self myStoryBoard] 
    selector:@selector(instantiateViewControllerWithIdentifier:) 
    parameters:^(TyphoonMethod *factoryMethod) {

        [factoryMethod injectParameterWith:@"viewControllerId"];
    }];

You should now be able to resolve this component from the factory. The documentation for this feature is here.

Incidentally, I note that you're resolving you controller using the TyphoonComponentFactory interface, which is fine. But did you know that the TyphoonComponentFactory can pose as any of your assembly interfaces? So you can also resolve as follows:

UIViewController* viewController = [(MyAssemblyType*) factory firstVc];

. . . this is especially useful for the following:

  • So that you can register more than one component of the same type, and resolve it without "magic strings".
  • To have a looser coupling on Typhoon, when you inject the factory itself.

Example:

@interface MyListViewController

//In the assembly we inject 'self'. 
//We'll obtain the detail VC using the "domain specific" assembly interface. 
//. . but when injecting self, it can be cast to TyphoonComponentFactory or any of your 
//assembly interfaces
@property(nonatomic, strong, readonly) MyAssembly* assembly;  

@end


来源:https://stackoverflow.com/questions/24223721/typhoon-with-storyboard-instantiating-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!