台风故事板:将 IBOutlet 视图注入控制器依赖项
Typhoon Storyboard: Inject an IBOutlet View to a Controller dependency
我有一个故事板,其中有一个视图使用插座连接到他的控制器。
在同一个控制器中,我想注入一个需要访问该视图的对象。我不想将该视图手动传递给对象,而是想自动注入它,但我不知道如何以及是否可以使用当前代码结构实现它。
class LoadingViewController: UIViewController {
@IBOutlet weak var loadingView: UIActivityIndicatorView!
private(set) var loadingViewModel: LoadingViewModel! // Dependency Injection
}
// Assembly
dynamic func loadingViewController() -> AnyObject {
return TyphoonDefinition.withClass(LoadingViewController.self) {
(definition) in
definition.injectProperty("loadingViewModel", with:self.loadingViewModel())
}
}
dynamic func loadingViewModel() -> AnyObject {
return TyphoonDefinition.withClass(LoadingViewModel.self) {
(definition) in
definition.injectProperty("loadingView", with:???) // I want loadingViewController.loadingView
}
}
我认为这与运行时间参数和循环依赖有关
这是一个很好的。我们必须考虑 Storyboard 创建的对象和 Typhoon 之间的生命周期。
你有没有尝试过类似的东西:
//The view controller
dynamic func loadingViewController() -> AnyObject {
return TyphoonDefinition.withClass(LoadingViewController.self) {
(definition) in
definition.injectProperty("loadingViewModel",
with:self.loadingViewModel())
definition.performAfterInjections("setLoadingViewModel", arguments: ) {
(TyphoonMethod) in
method.injectParameterWith(self.loadingViewModel())
}
}
}
dynamic func view() -> AnyObject {
return TyphoonDefinition.withFactory(self.loadingViewController(),
selector:"view")
}
dynamic func loadingViewModel() -> {
return TyphoonDefinition.withClass(SomeClass.class) {
(definition) in
definition.injectProperty("view", with:self.view())
}
}
- 为视图创建一个定义,指示 Typhoon 将从
loadingViewController
发出
- 为已注入
view
的 loadingViewModel
创建定义。
- 在
loadingViewController
和 view
创建之后,最后一步注入 loadingViewModel
。
我不记得在调用 performAfterInjections
之前是否清除了作用域池。如果是,您可能需要将 loadingViewController
的范围设置为 TyphoonScopeWeakSingleton
而不是默认的 TyphoonScopeObjectGraph
。
由于 Typhoon 和 Storyboards 之间的相互作用,手动提供实例可能更简单,例如 viewDidLoad
。但是你能试试上面的方法然后回复我吗?
我有一个故事板,其中有一个视图使用插座连接到他的控制器。 在同一个控制器中,我想注入一个需要访问该视图的对象。我不想将该视图手动传递给对象,而是想自动注入它,但我不知道如何以及是否可以使用当前代码结构实现它。
class LoadingViewController: UIViewController {
@IBOutlet weak var loadingView: UIActivityIndicatorView!
private(set) var loadingViewModel: LoadingViewModel! // Dependency Injection
}
// Assembly
dynamic func loadingViewController() -> AnyObject {
return TyphoonDefinition.withClass(LoadingViewController.self) {
(definition) in
definition.injectProperty("loadingViewModel", with:self.loadingViewModel())
}
}
dynamic func loadingViewModel() -> AnyObject {
return TyphoonDefinition.withClass(LoadingViewModel.self) {
(definition) in
definition.injectProperty("loadingView", with:???) // I want loadingViewController.loadingView
}
}
我认为这与运行时间参数和循环依赖有关
这是一个很好的。我们必须考虑 Storyboard 创建的对象和 Typhoon 之间的生命周期。
你有没有尝试过类似的东西:
//The view controller
dynamic func loadingViewController() -> AnyObject {
return TyphoonDefinition.withClass(LoadingViewController.self) {
(definition) in
definition.injectProperty("loadingViewModel",
with:self.loadingViewModel())
definition.performAfterInjections("setLoadingViewModel", arguments: ) {
(TyphoonMethod) in
method.injectParameterWith(self.loadingViewModel())
}
}
}
dynamic func view() -> AnyObject {
return TyphoonDefinition.withFactory(self.loadingViewController(),
selector:"view")
}
dynamic func loadingViewModel() -> {
return TyphoonDefinition.withClass(SomeClass.class) {
(definition) in
definition.injectProperty("view", with:self.view())
}
}
- 为视图创建一个定义,指示 Typhoon 将从
loadingViewController
发出
- 为已注入
view
的loadingViewModel
创建定义。 - 在
loadingViewController
和view
创建之后,最后一步注入loadingViewModel
。
我不记得在调用 performAfterInjections
之前是否清除了作用域池。如果是,您可能需要将 loadingViewController
的范围设置为 TyphoonScopeWeakSingleton
而不是默认的 TyphoonScopeObjectGraph
。
由于 Typhoon 和 Storyboards 之间的相互作用,手动提供实例可能更简单,例如 viewDidLoad
。但是你能试试上面的方法然后回复我吗?