标签栏没有select委托方法给予先前选定的标签索引在ios中,swift 3

我试图检测用户select哪个标签,实时。 作为一个例子,如果用户select0 th索引,同时我想让用户select第zeroth索引选项卡。 所以为此,我使用tabbarcontroller委托方法。

 override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { print("the selected index is : \(selectedIndex)") } 

但是这显示了以前的视图controller.as例如认为我在second tab ,然后我select第first tab然后这打印索引为2我怎样才能得到正确的select选项卡。 希望你的帮助。

你可以通过从UITabBar的项目数组中获取特定项目的位置来获得所选UITabBarItem的索引。 试试这个

 override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { print("the selected index is : \(tabBar.items.index(of: item))") } 

Swift 3.1

  override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { guard let items = tabBar.items else { return } print("the selected index is : \(String(describing: items.index(of: item)))") }