I dont have storyboard in my app. So all my dependencies spelled out in App Delegate. I read the documentation of the Swinject and there the option is suggested:
window.rootViewController = container.resolve(PersonViewController.self)
But in my case, the root controller is the tab bar controller (MainTabBarController()
), which sets the layout of my collections view and other options. How can this be fixed?
Initially, I tried to do everything through the Swinject Storyboard, but storyboardInitCompleted() just dont worked for me and my viewModel: ViewModelProtocol!
remained nil
(Resolution failed in console).
App Delegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let container: Container = {
let container = Container()
container.register(UnsplashService.self){ _ in UnsplashService()}//.inObjectScope(.container)
container.register(UnsplashManagerProtocol.self){ r in UnsplashDataFetcher(with: r.resolve(UnsplashService.self)!)}//.inObjectScope(.container)
container.register(ViewModelProtocol.self){ r in ViewModel(withClient: r.resolve(UnsplashManagerProtocol.self)!)}//.inObjectScope(.container)
container.register(PhotosCollectionViewController.self) { r in
let controller = PhotosCollectionViewController()
controller.viewModel = r.resolve(ViewModelProtocol.self)
return controller
}
return container
}()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let homeVC = UIViewController()
homeVC.view.backgroundColor = .white
window!.rootViewController = MainTabBarController()
window!.makeKeyAndVisible()
//window.rootViewController = container.resolve(PhotosCollectionViewController.self)
return true
}
MainTabBarController:
class MainTabBarController: UITabBarController {
var photosVC: PhotosCollectionViewController!
var likesVC: LikesCollectionViewController!
override func viewDidLoad() {
super.viewDidLoad()
//view.backgroundColor = .white
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
photosVC = PhotosCollectionViewController(collectionViewLayout: WaterfallLayout())
likesVC = LikesCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
viewControllers = [
generateNavigationController(rootViewController: photosVC, title: "Photos", image: #imageLiteral(resourceName: "photos")),
generateNavigationController(rootViewController: likesVC, title: "Favourites", image: #imageLiteral(resourceName: "heart"))
]
}
private func generateNavigationController(rootViewController: UIViewController, title: String, image: UIImage) -> UIViewController {
let navigationVC = UINavigationController(rootViewController: rootViewController)
navigationVC.tabBarItem.title = title
navigationVC.tabBarItem.image = image
return navigationVC
}
}
PhotosCollectionViewController:
class PhotosCollectionViewController: UICollectionViewController {
var viewModel: ViewModelProtocol!
...
}