Typhoon with Storyboard, instantiating ViewController

落爺英雄遲暮 提交于 2019-12-02 04:05:33

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