2017-01-03 11 views
0

ユーザーが選択したタブをリアルタイムで検出しようとしています。例として、ユーザが0 thインデックスを選択した場合、同時にそのユーザにzerothインデックスタブを選択させたいと思う。そのために、私はtabbarcontrollerデリゲートメソッドを以下のように使用しました。そして、これは私が正しい選択したタブを取得する方法の.so 2としてインデックスを印刷しタブバーはデリゲートメソッドを選択しました。以前に選択したタブインデックスをios、swift 3に与えました

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

     print("the selected index is : \(selectedIndex)") 
    } 

が、これは前のビューには例をcontroller.as示し、私はsecond tabでだと思うし、私はfirst tabを選択します。 これであなたの助けを願っています。

+0

チェックアウト私の答えを。 –

答えて

5

UITabBarの項目の配列から特定の項目の位置を取得することによって、選択されたUITabBarItemのインデックスを取得することができます。

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

thanxとnice。 – bill

2

スウィフト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)))") 
} 
関連する問題