在我的演示项目中,我将视图控制器的手动创建替换为在程序集中基于工厂的创建(如 Jasper Blues 所示: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")
})
}
但是视图控制器仍然需要一些其他的依赖。这样做时如何注入属性?
实际上我有两个问题:当我尝试像这样使用视图控制器时,所有 IBOutlets 都是 nil。我错过了什么吗?
根据 Jasper 的回答正确回答
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())
}
}