当前位置 : 主页 > 手机开发 > 其它 >

依赖注入 – 使用带有视图控制器初始化器的故事板的台风

来源:互联网 收集:自由互联 发布时间:2021-06-22
我想用故事板创建一个名为“child”的视图控制器,所以我定义: - (TyphoonStoryboard *)storyBoard{ return [TyphoonDefinition withClass:[TyphoonStoryboard class] configuration:^(TyphoonDefinition* definition) { [definiti
我想用故事板创建一个名为“child”的视图控制器,所以我定义:

- (TyphoonStoryboard *)storyBoard
{
    return [TyphoonDefinition withClass:[TyphoonStoryboard class] configuration:^(TyphoonDefinition* definition) {
        [definition useInitializer:@selector(storyboardWithName:factory:bundle:) parameters:^(TyphoonMethod *initializer) {
            [initializer injectParameterWith:@"Storyboard"];
            [initializer injectParameterWith:self];
            [initializer injectParameterWith:[NSBundle mainBundle]];
        }];
        definition.scope = TyphoonScopeSingleton; //Let's make this a singleton
    }];
}

- (ChildViewController *)childViewControllerFromStoryboard
{
    return [TyphoonDefinition withFactory:self.storyBoard selector:@selector(instantiateInitialViewController)];
}

我将有一个将包含子视图控制器的根视图控制器

- (RootViewController *)rootViewController {
    return [TyphoonDefinition withClass:[RootViewController class] configuration:^(TyphoonDefinition *definition) {
        [definition useInitializer:@selector(initWithChildViewController:) parameters:^(TyphoonMethod *initializer) {
            [initializer injectParameterWith:self.childViewControllerFromStoryboard];
        }];
    }]; 
}

最后一部分只是为子视图控制器注入依赖项:

- (ChildViewController *)childViewController {
    return [TyphoonDefinition withClass:[ChildViewController class] configuration:^(TyphoonDefinition *definition) {
        // Initilization doesn't work.
        [definition useInitializer:@selector(initWithData:) parameters:^(TyphoonMethod *initializer) {
            [initializer injectParameterWith:self.data];
        }];
        // Property injection does.
        // [definition injectProperty:@selector(data)]
    }];
}

超级正常,我只是使用它的初始化器,但它永远不会被调用.我认为这是因为始终使用initWithCoder:指定用于故事板的那个.

问题:

>可以在故事板中使用视图控制器初始化程序吗?它看起来像没有,类似于appDelegate.
>这是直接从装配中使用故事板的最简单方法吗?
>如果我有几个故事板,有没有一种方法可以参数化地定义故事板?我不想最终得到像storyBoard1,storyBoard2,…,storyBoardN这样的东西.

提前致谢.

感谢您对Typhoon的兴趣!

  1. Is possible to use view controller initializers with storyboards? It looks like no, similar to appDelegate.

不,你不能初始化对象两次(你可以调用init方法两次,但它奇怪和不正确,可能导致错误).

从Xib / Storyboard创建的每个UIViewController和UIView都使用initWithCoder:initialization方法.

相反,您可以使用方法注入并注入setData:或使用属性注入.

  1. Is this the simplest approach to use storyboards directly from the assembly?

最简单的方法是使用plist bootstrapping. (当Info.plist中指定的初始故事板名称时)

检查指南:

https://github.com/typhoon-framework/Typhoon/wiki/Obtaining-Built-Components#first-bootstrap-typhoon-using-either

但是如果你有更多的故事板,那么你可以在plist中指定initial,在assembly中指定其他.

  1. If I have several storyboards, is there a way of defining the storyboards parametrically? I don’t want to end up with something like storyBoard1, storyBoard2, …, storyBoardN.

是的,您可以使用运行时参数台风功能.
使用运行时参数,您的定义变为

- (TyphoonStoryboard *)storyBoardWithName:(NSString *)name
{
    return [TyphoonDefinition withClass:[TyphoonStoryboard class] configuration:^(TyphoonDefinition* definition) {
        [definition useInitializer:@selector(storyboardWithName:factory:bundle:) parameters:^(TyphoonMethod *initializer) {
            [initializer injectParameterWith:name]; // <-- runtime argument injection
            [initializer injectParameterWith:self];
            [initializer injectParameterWith:[NSBundle mainBundle]];
        }];
     }];
}

这是你要求的吗?

网友评论