简体   繁体   中英

TabBar Controller With Coordinator Pattern

I've been following Paul Hudson tutorial about the coordinator pattern and came to a 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)
    }
}

Issue occurs when I try to move from theses individual controllers.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM