ios 具有协调器模式的TabBar控制器

46scxncf  于 2023-03-14  发布在  iOS
关注(0)|答案(1)|浏览(105)

我一直在遵循Paul哈德逊关于coordinator模式的教程,遇到了一个必须使用选项卡栏控制器的示例。在显示选项卡栏控制器后,即使我有导航控制器示例,我也无法从一个地方移动到另一个地方。
我有一个主协调员来启动申请流程:

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

当我尝试从这些单独的控制器中移动时,出现问题。

w6lpcovy

w6lpcovy1#

找到了我要找的问题。

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

相关问题