0

I'm creating a UITabbarController and all its tab's contents programmatically, like so:

private func createTabBarController()
{
    /* Only create tabBarController once! */
    if (RootRouter._tabBarController == nil)
    {
        let firstPageViewController = FirstPageViewController(nibName: "FirstPageViewController", bundle: nil)
        let secondPageViewController = SecondPageViewController(nibName: "SecondPageViewController", bundle: nil)
        let thirdPageViewController = ThirdPageViewController(nibName: "ThirdPageViewController", bundle: nil)
        let thirdPageNavigationController = ThirdPageNavigationController(rootViewController: thirdPageViewController)
        let fourthPageViewController = FourthPageViewController(nibName: "FourthPageViewController", bundle: nil)

        thirdPageViewController.loadViewIfNeeded()

        RootRouter._tabBarController = UITabBarController()
        RootRouter._tabBarController?.viewControllers =
        [
            firstPageViewController,
            secondPageViewController,
            thirdPageNavigationController,
            fourthPageViewController
        ]

        /* This shouldn't be necessary! */
        let tabCount = RootRouter._tabBarController!.viewControllers?.count ?? 0
        for i in 0 ..< tabCount
        {
            RootRouter._tabBarController?.selectedIndex = i
        }
    }
}

If I comment-out the last part in this method the tabs won't be initialized properly on app start: Only the first three tab buttons are displayed and none of them are highlighted.

If the last code part is enabled it will work and look correct however the approach seems like a hack and I think might lead to side effects later. Is there anything I'm missing to initialize all tabs (and tab buttons) correctly?

BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
  • 1
    According to the developer documentation when you set UITabBarController.viewControllers you should also set selectedViewController or selectedIndex. So you should at least set selectedIndex once. Have you tried just setting to one value rather than looping through all vac's? – Dale May 13 '19 at 04:13

1 Answers1

1

selectedIndex

This property nominally represents an index into the array of the viewControllers property. However, if the selected view controller is currently the More navigation controller, this property contains the value NSNotFound

You have to select any index once.

Instead of doing this

 let tabCount = RootRouter._tabBarController!.viewControllers?.count ?? 0
 for i in 0 ..< tabCount
    {
            RootRouter._tabBarController?.selectedIndex = i
    }

Do like that:

 RootRouter._tabBarController?.selectedIndex = 0 // Any other index

There is nothing wrong in your approach, But the iterative call of RootRouter._tabBarController?.selectedIndex not required.

Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
  • 1
    You mean "iterative", not "recursive". – rmaddy May 13 '19 at 04:21
  • @rmaddy Right thanks for the correction. Updated :D – Hitesh Surani May 13 '19 at 04:23
  • Thank for the explanation but nope, that solution doesn't change the behavior in my case. If I set `selectedIndex` to 0 it will be as described above. If I set it to 3 it will show the fourth tab bar item but the initially navigated one isn't highlighted. – BadmintonCat May 13 '19 at 04:49
  • @iMHiteshSurani In any case, thanks for pointing out that my approach isn't wrong. I'm using `ReSwiftRouter` in this code and the odd behavior is likely due to that so the initialization loop as in my example above solves that. – BadmintonCat May 14 '19 at 01:23