【问题标题】:Typhoon with Storyboard, instantiating ViewControllerTyphoon 与 Storyboard,实例化 ViewController
【发布时间】:2014-06-15 14:52:39
【问题描述】:

在 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 能够从情节提要中获取对我想要的控制器的引用。

【问题讨论】:

    标签: storyboard typhoon


    【解决方案1】:

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

    //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
    

    【讨论】:

    • 是否可以将工厂初始化与属性注入结合使用?我想用情节提要的程序集连接我的 VC,但它还需要另一个依赖项。这可能吗,还是我需要为我的 VC 自定义初始化程序?
    • 是的,你可以。编写 definition.factory = [assembly someComponent] 。 .有关更多详细信息,请单独提问。
    猜你喜欢
    • 2015-12-01
    • 2012-02-27
    • 2015-05-30
    • 1970-01-01
    • 2023-03-25
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    相关资源
    最近更新 更多