选项卡栏选择委托方法在ios,swift 3中给出以前选择的选项卡索引

bil*_*ill 5 selectedindex uitabbarcontroller ios swift3

我正在尝试实时检测用户选择的哪个选项卡.例如,如果用户选择0 th索引,同时我想让该用户选择zeroth索引选项卡.所以,为此,我使用了tabbarcontroller如下的委托方法.

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

        print("the selected index is : \(selectedIndex)")
    }
Run Code Online (Sandbox Code Playgroud)

但这显示了以前的视图控制器.作为一个例子,我想我在second tab然后选择然后我first tab将索引打印为2.so如何才能获得正确的选定选项卡.希望你对此有所帮助.

Vis*_*kar 8

您可以UITabBarItem通过从项目数组中获取该特定项目的位置来获取所选索引UITabBar.试试吧

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