台风与故事板,实例化ViewController

时间:2014-06-14 19:53:21

标签: storyboard typhoon

在Typhoon中使用故事板时,如果我在程序集中执行类似的操作

- (id)myController
{

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

后来我希望工厂从Typhoon故事板给我控制器,但我最终得到了使用alloc / init创建的普通控制器

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

在AppDelegate中我使用台风故事板如下

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

我可以回到使用StoryboardWithIdentifier ......但是我想使用_factory能够从故事板中获取我想要的控制器的引用。

1 个答案:

答案 0 :(得分:3)

您是否尝试将故事板声明为工厂组件?这是一个例子:

//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"];
    }];

您现在应该可以从工厂解决此组件了。 此功能的文档is here

顺便提一下,我注意到您正在使用TyphoonComponentFactory界面解析您的控制器,这很好。但是你知道TyphoonComponentFactory可以构成你的任何汇编接口吗?所以你也可以按如下方式解决:

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

。 。 。这对以下内容特别有用:

  • 这样您就可以注册多个相同类型的组件,并在没有"魔术字符串"的情况下解决它。
  • 当你注射工厂本身时,在Typhoon上有一个更松散的耦合。

示例:

@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