如何在 UITabBarController 中实现协调器

how can implement coordinator in UITabBarController

我有一个以编程方式创建的 UITabBarViewController,我已经导入了我所有的 3 个 UIViewController,现在我想将 Coordinator 添加到我的项目中,我想将 Coordinator 添加到我的所有 3 个 UIViewController。 我该如何使用它?

class MainTabbarViewController: UITabBarController, UITabBarControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // home Tab
        let homeStoryboard: UIStoryboard = UIStoryboard(name: "HomeStoryboard", bundle: nil)
        let homeTab = homeStoryboard.instantiateViewController(withIdentifier: "HomeViewController")
        let homeTabBarItem = UITabBarItem(title: "Home", image: UIImage(systemName: "house"), selectedImage: UIImage(systemName: "house.fill"))
        homeTab.tabBarItem = homeTabBarItem
        
        // search Tab
        let searchStoryboard: UIStoryboard = UIStoryboard(name: "SearchStoryboard", bundle: nil)
        let searchTab = searchStoryboard.instantiateViewController(withIdentifier: "SearchViewController")
        let searchTabBarItem = UITabBarItem(title: "Search", image: UIImage(systemName: "magnifyingglass"), selectedImage: UIImage(systemName: "magnifyingglass"))
        searchTab.tabBarItem = searchTabBarItem
        
        // profile Tab
        let profileStoryboard: UIStoryboard = UIStoryboard(name: "ProfileStoryboard", bundle: nil)
        let profileTab = profileStoryboard.instantiateViewController(withIdentifier: "ProfileViewController")
        let profileTabBarItem = UITabBarItem(title: "Profile", image: UIImage(systemName: "person"), selectedImage: UIImage(systemName: "person.fill"))
        profileTab.tabBarItem = profileTabBarItem
        profileTab.navigationController?.navigationBar.isHidden = true
        self.tabBar.tintColor = UIColor(asset: Asset.Colors.yellow)
        self.viewControllers = [homeTab, searchTab, profileTab]
    }
}

这是我的 UITabBarController

protocol Coordinator {
    var childCoordinators: [Coordinator] { get set }
    var navigationController: UINavigationController { get set }

    func start()
}

这是我的协调器协议。

此文件应删除并添加其他文件。 我用这个代码命名了 TabCoordinator:

final class TabCoordinator: NSObject, TabBarCoordinatorProtocol {
    // Root View Controller
    var rootViewController: UIViewController {
        return tabController
    }
    // Empty UITabBarController
    let tabController: UITabBarController
    
    // Tab Item Coordinators
    let homeCoordinator: HomeCoordinator
    let searchCoordinator: SearchCoordinator
    let profileCoordinator: ProfileCoordinator
    // Initialize
    
    override init() {
        tabController = UITabBarController()
        var controllers: [UIViewController] = []
        
        // Home Coordinator
        homeCoordinator = HomeCoordinator(navigationController: UINavigationController())
        let homeViewController = homeCoordinator.rootViewController
        homeViewController.tabBarItem = UITabBarItem(title: "Home", image: UIImage(systemName: "house"), selectedImage: UIImage(systemName: "house.fill"))
        homeCoordinator.start()
        
        // Search Coordinator
        searchCoordinator = SearchCoordinator(navigationController: UINavigationController())
        let searchViewController = searchCoordinator.rootViewController
        searchViewController.tabBarItem = UITabBarItem(title: "Search", image: UIImage(systemName: "magnifyingglass"), selectedImage: UIImage(systemName: "magnifyingglass"))
        searchCoordinator.start()
        
        // Profile Coordinator
        profileCoordinator = ProfileCoordinator(navigationController: UINavigationController())
        let profileViewController = profileCoordinator.rootViewController
        profileViewController.tabBarItem = UITabBarItem(title: "Profile", image: UIImage(systemName: "person"), selectedImage: UIImage(systemName: "person.fill"))
        profileCoordinator.start()
        super.init()
        
        controllers.append(homeViewController)
        controllers.append(searchViewController)
        controllers.append(profileViewController)
        tabController.viewControllers = controllers
        tabController.tabBar.tintColor = UIColor(asset: Asset.Colors.yellow)
        tabController.tabBar.isTranslucent = false
    }
}

TabBarCoordinatorProtocol 文件包含此代码

public protocol TabBarCoordinatorProtocol {
    var rootViewController: UIViewController { get }
}