Typhoon Storyboard:将IBOutlet视图注入Controller依赖项

时间:2015-01-23 11:01:21

标签: ios typhoon

我有一个故事板,其中有一个视图,使用插座连接到他的控制器。 在同一个控制器中,我想注入一个需要访问该视图的对象。而不是手动将该视图传递给对象我想自动注入它,但我不知道如何以及如果我可以用当前的代码结构实现它。

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 
    }
}

我认为它与运行时参数和循环依赖

有关

1 个答案:

答案 0 :(得分:1)

这是一个很好的。我们必须考虑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发出它
  • loadingViewModel注入的view创建定义。
  • 在创建loadingViewController后创建view后,请注入loadingViewModel作为最后一步。

在调用performAfterInjections之前,我不记得范围池是否已清除。如果是,您可能需要将loadingViewController的范围设置为TyphoonScopeWeakSingleton,而不是默认TyphoonScopeObjectGraph

由于Typhoon和Storyboard之间的相互作用,在viewDidLoad中手动提供实例可能更为简单。但你可以尝试上面的尝试并回复我吗?

相关问题