簡體   English   中英

帶協調器模式的 TabBar Controller

[英]TabBar Controller With Coordinator Pattern

我一直在關注有關協調器模式的 Paul Hudson 教程,並遇到了一個實例,在該實例中,即使我有導航 controller 實例,我也必須在顯示標簽欄 controller 后使用標簽欄 controller 我無法從一個地方移動到另一個地方其他。

我有一個主要協調員來啟動申請流程。

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

我的標簽欄協調員為每個頁面部分都有單獨的協調員,這是我的標簽欄協調員。

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

例如,這是上面提到的個人協調員

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

當我嘗試從這些單獨的控制器中移出時會出現問題。

找到了我正在尋找的問題。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM