带有附加属性注入的工厂初始化 [英] Factory initialization with additonal property injection
问题描述
在我的演示项目中,我像这样在程序集中将视图控制器的手动创建替换为基于工厂的创建(如Jasper Blues所示:https://stackoverflow.com/a/24227246/397898 )
In my demo project I replaced the manual creation of a view controller with the factory-based creation within an assembly like so (as Jasper Blues demonstrated here: https://stackoverflow.com/a/24227246/397898)
// ApplicationAssembly
dynamic func mainStoryboard() -> AnyObject {
return TyphoonDefinition.withClass(TyphoonStoryboard.self) {
(definition) in
definition.useInitializer("storyboardWithName:factory:bundle:") {
(initializer) in
initializer.injectParameterWith("Main")
initializer.injectParameterWith(self)
initializer.injectParameterWith(NSBundle.mainBundle())
}
definition.scope = TyphoonScope.Singleton
}
}
// PersonListAssembly
dynamic func personListViewController() -> AnyObject {
return TyphoonDefinition.withFactory(self.applicationAssembly.mainStoryboard(), selector: "instantiateViewControllerWithIdentifier:", parameters: {
(factoryMethod) in
factoryMethod.injectParameterWith("PersonListViewController")
})
}
但是视图控制器仍然需要其他一些依赖.这样做时我该如何注入物产?
But the view controller still needs some other dependencies. How can I inject the propery when doing this?
实际上我有两个问题:当我尝试使用这种视图控制器时,所有IBOutlets都为零.我想念什么吗?
And actually I have two questions: All IBOutlets are nil, when I try to use the view controller like this. Am I missing something?
根据贾斯珀的回答正确回答
dynamic func personListViewController() -> AnyObject {
return TyphoonDefinition.withClass(PersonListViewController.self) {
(definition) in
definition.factory = self.applicationAssembly.mainStoryboard()
definition.useInitializer("instantiateViewControllerWithIdentifier:", parameters: { (factoryMethod) in
factoryMethod.injectParameterWith("PersonListViewController")
})
definition.injectProperty("presenter", with: self.personListPresenter())
}
}
推荐答案
似乎,声明从另一个Typhoon组件发出的组件的快捷方式尚不支持此功能.我们将打开问题.同时,您可以使用Typhoon 1.0样式的API:
It seems the shortcut way of declaring a component that is emitted from another Typhoon component does not support this (yet). We'll open an issue. Meanwhile you can fall back to Typhoon 1.0 style API:
- (id)currentTheme
{
return [TyphoonDefinition withClass:[PFTheme class]
configuration:^(TyphoonDefinition* definition)
{
definition.factory = [self themeFactory];
[definition useInitializer:@selector(sequentialTheme)];
}];
}
. .使用这种方法时:
. . when you use this approach:
- 初始化程序(带有或不带有args)实际上是对象上的实例方法,该方法将产生组件.
- 可以设置其他属性,范围等.
我们创建了一种简短的方法,因为它有点引人注意初始化程序",而初始化程序"实际上是创建组件的类上的一个实例方法.似乎在添加配置块方面存在疏忽.
We created the short way as its a little confusing to refer to an 'initializer' that is in fact an instance method on the class creating the component. It looks like there was an oversight in adding a configuration block.
这篇关于带有附加属性注入的工厂初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!