UITabBar 控制器给出以前选择的索引? [英] UITabBar controller gives previous selected index?

查看:25
本文介绍了UITabBar 控制器给出以前选择的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用自定义 UITabBarController 来检查选定的索引.当我选择 selectedIndex 时,我有这个方法来触发:

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?

推荐答案

实际上 selectedIndexdidSelect 发生之前不会改变.商品索引应取自商品本身.

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))")
}

这篇关于UITabBar 控制器给出以前选择的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆