问题
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