tabBarController didSelect does not get called

妖精的绣舞 提交于 2020-01-06 06:12:16

问题


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!


回答1:


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!




回答2:


Tabbar delegate is called only in response to user taps in the tab bar and is not called when your code changes the tab bar contents programmatically.



来源:https://stackoverflow.com/questions/54227742/tabbarcontroller-didselect-does-not-get-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!