带协调器的选项卡栏控制器(MVVM-C)-在不同选项卡中打开相同的视图控制器

fquxozlt  于 2022-10-04  发布在  Swift
关注(0)|答案(1)|浏览(170)

我有一个带有两个选项卡的TabBar协调器:一个是带有FirstTabCoordinator的电影列表(MoviesListViewController),另一个是带有SecondTabCoordinator的最喜欢的电影(FavoritesViewController),这两个选项卡都有一个表视图,每个选项卡都有自己的navigationController。当我点击一部电影时,在电影列表或收藏夹选项卡中,我会按下MovieDetailsViewController。这个很好用。但在MovieDetailsViewController中,我有另一个类似电影的集合。

我的问题是:

在MovieDetailsViewController中,当我在didSelectItemAt indexPath上点击类似的电影时,我如何判断在哪个协调器中推送下一个MovieDetailsViewController?因为我可以从两位协调人那里了解到电影的细节。

我想我可能不得不在这两个协调人之间增加某种机制,但我不确定。

这是我的主协调员

protocol Coordinator {
  func startCoordinator()
}

class MainCoordinator: Coordinator {
  var tabBarController = UITabBarController()

  static func getCoordinator() -> MainCoordinator? {
    if let scene = UIApplication.shared.connectedScenes.first, let delegate = scene.delegate as? SceneDelegate {
      return delegate.coordinator
    }
    return nil
  }

  func startCoordinator() {
    FirstTabCoordinator.shared.startCoordinator()
    let moviesListVC = FirstTabCoordinator.shared.moviesListViewController
    self.setupTabBarItem(vc: moviesListVC,
                     title: "All Movies",
                     imageName: "film",
                     selectedImageName: "film.fill")

    SecondTabCoordinator.shared.startCoordinator()
    let favoritesVC = SecondTabCoordinator.shared.favoritesViewController
    self.setupTabBarItem(vc: favoritesVC,
                     title: "Favorites",
                     imageName: "heart",
                     selectedImageName: "heart.fill")

    self.tabBarController.tabBar.barStyle = .black
    self.tabBarController.tabBar.isTranslucent = false
    self.tabBarController.tabBar.tintColor = .white

    self.tabBarController.viewControllers = [FirstTabCoordinator.shared.navigationController,
                                             SecondTabCoordinator.shared.navigationController]
  }

  func setupTabBarItem(vc: UIViewController, title: String, imageName: String, selectedImageName: String) {
    let defaultImage = UIImage(systemName: imageName)
    let selectedImage = UIImage(systemName: selectedImageName)
    let tabBarItem = UITabBarItem(title: title, image: defaultImage, selectedImage: selectedImage)
    vc.tabBarItem = tabBarItem
  }
}

第一页签协调器:

class FirstTabCoordinator: Coordinator {
  var navigationController: UINavigationController! {
    didSet {
      navigationController?.navigationBar.tintColor = .white
      navigationController?.navigationBar.barStyle = .black
    }
  }
  let storyboard = UIStoryboard(name: "Main", bundle: .main)
  private lazy var navigationManager = NavigationManager(self.navigationController)

  static let shared = FirstTabCoordinator()
  private init() {}

  lazy var moviesListViewController: MoviesTableViewController = {
    let viewController = self.storyboard.instantiateViewController(identifier: "MoviesTableViewController") as! MoviesTableViewController
    viewController.title = "All Movies"
    return viewController
  }()

  func startCoordinator() {
    self.navigationController = UINavigationController()
    self.navigationController.viewControllers = [self.moviesListViewController]
  }

  func goToMovieDetails(movie: Movie) {
    self.navigationManager.goToMovieDetails(movie: movie)
  }
}

第二页签协调器:

class SecondTabCoordinator: Coordinator {
  var navigationController: UINavigationController! {
    didSet {
      navigationController?.navigationBar.tintColor = .white
      navigationController?.navigationBar.barStyle = .black
    }
  }
  let storyboard = UIStoryboard(name: "Main", bundle: .main)
  private lazy var navigationManager = NavigationManager(self.navigationController)

  static let shared = SecondTabCoordinator()
  private init() {}

  lazy var favoritesViewController: FavoritesViewController = {
    let viewController = self.storyboard.instantiateViewController(identifier: "FavoritesViewController") as FavoritesViewController
    viewController.title = "Favorites"
    return viewController
  }()

  func goToMovieDetails(movie: Movie) {
    self.navigationManager.goToMovieDetails(movie: movie)
  }

  func startCoordinator() {
    self.navigationController = UINavigationController()
    self.navigationController.viewControllers = [self.favoritesViewController]
  }
}
watbbzwu

watbbzwu1#

您只需让协调器实现一个包含您的函数func goToMovieDetails(movie: Movie)的协议,并在创建视图控制器时传入它的一个示例,这样您就不必关心它是第一个还是第二个Tab协调器

相关问题