UITabBar controller gives previous selected index?

对着背影说爱祢 提交于 2020-05-15 09:03:39

问题


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

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