带有Storyboard的台风,实例化ViewController [英] Typhoon with Storyboard, instantiating ViewController

查看:97
本文介绍了带有Storyboard的台风,实例化ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Typhoon中使用情节提要时,如果我在程序集中做类似的事情

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

稍后,我希望工厂将台风故事板上的控制器交给我,但最后我还是使用alloc/init创建的普通控制器

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

在AppDelegate中,我按如下方式使用台风情节提要

In AppDelegate I am using the typhoon storyboard as follows

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

我可以回到使用StoryboardWithIdentifier的方式...但是我想使用_factory能够从情节提要中获取我想要的控制器的引用.

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.

推荐答案

您是否尝试将情节提要声明为工厂组件?这是一个示例:

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.

顺便说一句,我注意到您正在使用TyphoonComponentFactory接口解析控制器,这很好.但是您知道TyphoonComponentFactory可以构成您的任何装配接口吗?因此,您也可以按以下方式解决:

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:

  • 这样您可以注册多个相同类型的组件,并在不使用魔术字符串"的情况下解决该问题.
  • 在注入工厂本身时,可以使台风上的联轴器松动.

示例:

@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

这篇关于带有Storyboard的台风,实例化ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆