1

在台风中使用情节提要时,如果我在程序集中做这样的事情

- (id)myController
{

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

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

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

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

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

我可以回去使用 StoryboardWithIdentifier ......但我想使用_factory能够从情节提要中获取对我想要的控制器的引用。

4

1 回答 1

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

您现在应该能够从工厂解决此组件。此功能的文档在此处

顺便说一句,我注意到您正在使用 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
于 2014-06-15T06:52:39.070 回答