问题
I'm using Custom UITabBarController
in the app to check selected index. I've this method to trigger when I select the selectedIndex
:
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
// print(self.selectedIndex)
switch self.selectedIndex {
case 1:
print("should load feed " + String(self.selectedIndex))
(self.viewControllers![1] as? PageViewController)?.downloadNews()
case 2:
print("should load saved" + String(self.selectedIndex))
(self.viewControllers![2] as? SavedController)?.loadData()
default:
return
}
}
However I get old selected index when I click the new tab bar item. I know it's something easy any idea. Is there a way to takeout the right index in this method?
回答1:
Actually selectedIndex
is not changed until didSelect
happens.
Item index should be taken from the the item itself.
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
{
let indexOfTab = tabBar.items?.index(of: item)
print("pressed tabBar: \(String(describing: indexOfTab))")
}
来源:https://stackoverflow.com/questions/48192790/uitabbar-controller-gives-previous-selected-index