I am starting to use Swinject
in my apps and I want to share an instance of my rootNavigator between presenters
(Viper/MVP) or viewControllers
(Not Viper). The main problem that I have is that I want to share appRootNavigator
in presenters
, but I loose the instance of window
because when I register a new module swinject
generates a new instance of that class (AppRootNavigator
). How can I share AppRootNavigator
between presenters
.
In this part of the code, I try to change the navigator
of the presenter
, but it never goes into the if.
if let presenter = container.resolve(SignupPresenter.self, argument: self){
signUpController.presenter = presenter
}
I don't know another way to do that.
This is the rootNavigator
:
import Foundation
import UIKit
import Swinject
class AppRootNavigator: RootNavigator {
internal let container: Container
fileprivate var window: UIWindow?
init(container: Container) {
self.container = container
}
fileprivate lazy var storyboards: Storyboard = {
return Storyboard(container: self.container)
}()
func installRootViewController(in window:UIWindow) {
self.window = window
currentNavigationController = signupController()
self.window?.rootViewController = currentNavigationController
}
fileprivate func signupController() -> UINavigationController {
let signUpController = storyboards.signup.instantiateInitialViewController() as! SignupController
if let presenter = container.resolve(SignupPresenter.self, argument: self) {
signUpController.presenter = presenter
}
return UINavigationController.init(rootViewController: signUpController)
}
Here I register all elements I want to use.
import Swinject
import Alamofire
import RealmSwift
struct SignupContainer: Bundle {
func create(_ container: Container) {
container.register(SignupNavigator.self) { c in
AppRootNavigator.init(container: container) as SignupNavigator
}
container.register(SignupInteractor.self) { c in
SignupInteractorImpl(facebook: c.resolve(FacebookService.self)!,
api: c.resolve(API.self)!,
realm: c.resolve(Realm.self)!,
analytics: c.resolve(Tracker.self)!)
}
container.register(SignupPresenter.self) { c, navigator in
SignupPresenterImpl(navigator: navigator, interactor:c.resolve(SignupInteractor.self)!) as SignupPresenter
}
container.registerForStoryboard(SignupController.self) {
c, controller in
if var presenter = c.resolve(SignupPresenter.self, argument: c.resolve(SignupNavigator.self)!) {
controller.presenter = presenter
presenter.view = controller
}
}