ios - UITabBar Controller 提供先前选择的索引?

标签 ios swift uitabbarcontroller

我在应用程序中使用自定义 UITabBarController 来检查选定的索引。当我选择 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
    }
}

但是,当我单击新的标签栏项目时,我得到了旧的选定索引。我知道这很容易。有没有办法在这个方法中取出正确的索引?

最佳答案

实际上 selectedIndexdidSelect 发生之前不会改变。 项目索引应取自项目本身。

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
{ 
   let indexOfTab = tabBar.items?.index(of: item)
   print("pressed tabBar: \(String(describing: indexOfTab))")
}

关于ios - UITabBar Controller 提供先前选择的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48192790/

相关文章:

ios - 是否有任何理由将 UICollectionView 用于完全静态的 GridView ?

ios - Collection View - 从不同文件夹接收图像 - 如何?

ios - UITabBarController 图标未显示

php - 如何在 iOS 应用程序中保存用户头像

ios - 如何在 UITableView 周围画线

ios - 将图像旋转超过 180 度

ios - UIView subview 的动画效果不流畅

ios - 错误处理程序 : NO with WCErrorCodeDeliveryFailed - not sure where I am going wrong

iphone - 如何知道 StatusBar 大小何时发生变化 (iPhone)

ios - UINavigationController navBar 内的 UIPageViewController 中的 UITableViewController 不反弹