我一直在关注关于协调器模式的 Paul Hudson 教程,并遇到了一个实例,即使我有导航控制器实例,我也必须在呈现标签栏控制器后使用标签栏控制器,但我无法从一个地方移动到另一个地方其他。
我有一个主要协调员来启动应用程序流程。
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)
}
}
当我尝试从这些单独的控制器中移动时会出现问题。