Search code examples
iosuitabbarcontroller

tabBarController didSelect does not get called


I am having a problem with

tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)

delegate firing. The problem lies when I try to use self.tabBarController?.selectedIndex and change a tab programmatically. Once I use selectedIndex and go back to a previous tab and click on the tabBarItem the delegate does not fire anymore. Delegate only fires if I do not use selectedIndex but once i use it the didSelect delegate never fires again even if I tap on the tabBar item. Any suggestions? Thanks for your help!


Solution

  • You need to call delegate programmatically like below For eg. I need to select SettingsTab which is at 4th index, i can achieve using this code. Here didSelect is also called programmatically

    if let tabbarC = self.tabBarController{
            tabbarC.selectedIndex = 4
            let setting = tabbarC.viewControllers![4]
            self.tabBarController(tabbarC, didSelect: setting)
    
    }
    

    Hope this helps!