UITableViewController
ie: という名前のクラスのインスタンスをdidSelectRowAtSection
ロードする があり、それが のプロパティを持っているため、プロパティインジェクションClassToInject
を介してそれを注入しようとしていると仮定しましょう。次のように単純に準拠します。ViewControllerToBePushed
ClassToInject
UITabBarViewController
didSet
viewControllers
ClassToInjectPresentable
protocol ClassToInjectPresentable {
var property: ClassToInject { get set }
}
今まで、私は次のようなことをしていました:
func didSelectRowAtIndexPath {
let classToInject = self.loadClassToInjectFor(indexPath)
let tabBarViewController = SomeTabBarViewController()
tabBarViewController.property = classToInject
self.navigationController.push(tabBarViewController, animated: true)
}
そしてSomeTabBarViewController
...
class SomeTabBarViewController: ClassToInjectPresentable {
var property: ClassToInject? {
didSet(newValue) {
self.viewControllers.filter{ $0 is ClassToInjectPresentable }.map{ $0 as! ClassToInjectPresentable }.forEach{ $0.property = newValue }
}
}
そして、すべてがうまく簡単に読み込まれる必要があります (しかし、そうではありません)。について読んだことがSwinject
ありますが、これで解決する可能性があります。次のようなものを登録する多くの例を見てきました。
container.register(Animal.self) { _ in Cat(name: "Mimi") }
しかし、ロードされているプロパティを登録できるかどうかはわかりませんself
:
container.register(ClassToInjectInjector.self) { _ in
self.loadClassToInjectFor(indexPath) }
// And then
container.register(ClassToInjectPresentable.self) { _ in
SomeTabBarViewController() }
.initCompleted { r, p in
let tabBar = p as! SomeTabBarViewController
tabBar.property = r.resolve(ClassToInjectInjector.self)
// And lastly?
self.navigationController.pushViewController(tabBar, animated: true)
}
}