[英]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())
}
}
loadingViewController
發出它 view
的loadingViewModel
創建定義。 loadingViewController
,因此創建了view
,請注入loadingViewModel
作為最后一步。 我不記得在調用performAfterInjections
之前是否清除了作用域池。 如果是,您可能需要將loadingViewController
的范圍設置為TyphoonScopeWeakSingleton
而不是默認的TyphoonScopeObjectGraph
。
由於Typhoon和Storyboard之間的相互作用,在例如viewDidLoad
手動提供實例可能更簡單。 但你可以嘗試上面的嘗試並回復我嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.