2012-06-27 23 views
0

我想获得一个代码,我希望在appdelegate中选定的选项卡索引。我可以在我的视图控制器中得到这个,但是我怎么能在我的appdelegate中获得这个?在appdelegate中获取选定的选项卡

有人可以帮我做这个吗?

在此先感谢。

+1

可能重复的[UITabBar(Controller) - 获取挖掘索引?](http://stackoverflow.com/questions/3766230/uitabbarcontroller-get-index-of-tapped) – rishi

回答

0

可以使用MyTabBar.selectedIndex,放在宾语AppDelegateClass .. 或者,如果你已经的TabBar Avaliable 可以使用

AppDelegate *app = [[UIApplication sharedApplication] delegate]; 
NSLog(@"%i",app.Tab.selectedIndex); 

,如果你需要得到它在同一CALSS这样你就可以使用

NSLog(@"%i",self.Tab.selectedIndex); 

如果要防止用户去另一个选项卡,你可以使用委托

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ 
    if (tabBarController.selectedIndex == 3) { 
      //if the user will select tab 3 so user will not go to it 
     return NO; 
    }else{ 
      // if any other tab so return yse will let you to other tabs 
     return YES; 
    } 

} 
+0

我应该在哪里放这段代码? –

+0

在你想要获得选中标签的类中..只需导入你的applicationdelegate类并使用这段代码来访问选定的标签栏 –

+0

但是,好友,如果我想要在appdelegate类中获得它,可能吗?导致实际上要防止用户针对某些情况访问视图控制器。 –

相关问题