iosswiftdesign-patternscoordinator-pattern

TabBar Controller with Coordinator Pattern


I've been following Paul Hudson tutorial about the coordinator pattern and came to an instance where I have to use a tab bar controller. After presenting the tab bar controller even if I have the navigation controller instances I can't move from one place to a another.

I have a Main coordinator to start the application flow:

class MainCoordinator: Coordinator {
    
    var childCoordinators = [Coordinator]()
    var navigationController: UINavigationController
    
    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }
    
    func start() {
        let vc = ViewController.instantiate(storyboard: .main)
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: false)
    }
        
    func navigateToCreateAccount(){
        let vc = CreateAccountVC.instantiate(storyboard: .main)
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    }
    
    func navigateToBuySubscription(){
        let vc = BuySubscriptionVC.instantiate(storyboard: .main)
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    }
    
    
    func navigateToTabBar(){
        let vc = TabBarCoordinator(navigationController: navigationController)
        childCoordinators.append(vc)
        vc.start(type: .LoginSession)
    }
}

And my Tab Bar coordinator has individual coordinators for each page section this is my tab bar coordinator:

class TabBarCoordinator: Coordinator {
    
    var childCoordinators = [Coordinator]()
    var navigationController: UINavigationController
    
    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }
    
    func start(type: NavigationType) {
        
        let vc = NewsFeedTBC.instantiate(storyboard: .newsfeed)
        
        //TODO: check the need of assigning this navigation controller
        
        vc.coordinator = self
        
        //TopRated VC Related
        let topRatedNavigationController = UINavigationController()
        topRatedNavigationController.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 0)
        let topRatedCoordinator = TopRatedCoordinator(navigationController: topRatedNavigationController)
        topRatedCoordinator.start()
    
        //Bookmarks VC Related
        let bookmarksNavigationController = UINavigationController()
        bookmarksNavigationController.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 1)
        let bookmarksCoordinator = BookmarksCoordinator(navigationController: bookmarksNavigationController)
        bookmarksCoordinator.start()
        
        //Downloads VC Related
        let downloadsNavigationController = UINavigationController()
        downloadsNavigationController.tabBarItem = UITabBarItem(tabBarSystemItem: .downloads, tag: 2)
        let downloadsCoordinator = DownloadsCoordinator(navigationController: downloadsNavigationController)
        downloadsCoordinator.start()
        
        vc.modalPresentationStyle = .fullScreen
        
        vc.viewControllers = [
            topRatedNavigationController,
            downloadsNavigationController,
            bookmarksNavigationController
        ]
 
        type == .LoginSession ? navigationController.present(vc, animated: true, completion: nil) : navigationController.pushViewController(vc, animated: true)
    }
}

For example this is an above mentioned individual coordinator:

class BookmarksCoordinator: Coordinator {
    
    var childCoordinators = [Coordinator]()
    var navigationController: UINavigationController
    
    
    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }
    
    func start() {
        let vc = BookMarksVC.instantiate(storyboard: .newsfeed)
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    }
    
    func navigateToProfile(){
        let vc = ProfileVC.instantiate(storyboard: .newsfeed)
        vc.coordinator = self
        navigationController.present(vc, animated: true, completion: nil)
    }
}

The issue occurs when I try to move from theses individual controllers.


Solution

  • Found The Issue that I was searching for.

    func start() {
        
        let vc = NewsFeedTabBarVC.instantiate(storyBoard: .NewsFeed)
        vc.coordinator = self
        vc.sideMenuDelegate = self
        
        //Home VC Related
        let homeNavigationController = UINavigationController()
        homeNavigationController.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "home"), tag: 0)
        let homeCoordinator = HomeNavigator(navigationController: homeNavigationController)
        childCoordinators.append(homeCoordinator)
        homeCoordinator.avatarDelegate = vc
        homeCoordinator.start()
        
        //Friends VC Related
        let friendsNavigationController = UINavigationController()
        friendsNavigationController.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "users"), tag: 1)
        let friendsCoordinator = FriendsNavigator(navigationController: friendsNavigationController)
        childCoordinators.append(friendsCoordinator)
        
        friendsCoordinator.start()
    }