Search code examples
iosswiftuitabbarcontrollerprogrammatically-createdcoordinator-pattern

Creating UITabBarController programmatically, only one Tab is showing up


I have a TabBarCoordinator, thats contains a UITabBarController.

I want to add two UINavigationController (CoinPage and Top/Flop), but only one is showing up in the Tab. I thought I have set up everything the right way and I tried some other stuff but I can not get it to work.

enter image description here

This is my code:

class TabBarCoordinator: Coordinator {
    var dependencys: DependencyManager


    let tabBarController: UITabBarController
    var tabCoordinators = [Tabs: Coordinator]()
    var navigationController: UINavigationController

    init(navigationController: UINavigationController, persistenceCentral: PersistenceCentral, dependencys: DependencyManager) {
        self.tabBarController = UITabBarController()
        self.navigationController = navigationController
        self.dependencys = dependencys
        var controllers: [UIViewController] = []

        tabCoordinators[.topFlop] = TopFlopCoordinator(navigationController: navigationController, dependencys: dependencys)

        tabCoordinators[.coinPage] = CoinPageCoordinator(dependencys: dependencys, navigationController: navigationController)

        let coinPageVC = tabCoordinators[.coinPage]!.navigationController
        coinPageVC.tabBarItem = UITabBarItem(title: "Coinpage", image: nil, tag: 0)


        let topFlopVC = tabCoordinators[.topFlop]!.navigationController
        topFlopVC.tabBarItem = UITabBarItem(title: "Top/Flop", image: nil, tag: 1)


        controllers.append(topFlopVC)
        controllers.append(coinPageVC)

        tabBarController.viewControllers = controllers
        tabBarController.tabBar.isTranslucent = false

        tabCoordinators[.topFlop]?.start()
        tabCoordinators[.coinPage]?.start()
    }

    func start() {

    }
}

I did some research, for example he sets up the tabs in a similar way: https://medium.com/@satindersingh71/uitabbarcontroller-programmatically-2a3df63607f1

So I do not understand why it is not working.


Solution

  • These 2 lines return the same object

     let coinPageVC = tabCoordinators[.coinPage]!.navigationController
     let topFlopVC = tabCoordinators[.topFlop]!.navigationController
    

    so the last overwrite settings of the first and it shown lonely , you have to make sure that you create 2 separate navigation objects