简体   繁体   中英

how can implement coordinator in UITabBarController

i have a UITabBarViewController that i have created programmatically and i had imported all my 3 UIViewControllers , Now I wanna add Coordinator to my Project and i want to add Coordinator to all my 3 UIViewControllers. how can i Use It?

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

this is my UITabBarController

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

    func start()
}

and this is my Coordinator protocol.

this file should remove and add an other file. i named TabCoordinator with this Code:

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

and TabBarCoordinatorProtocol file incloude this codes

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM