0

I want to get a code, that I want the selected tab index in appdelegate. I can get this in my view controller, but how can I get this in my appdelegate?

Can anybody help me out doing this?

Thanks in advance.

Soumalya Banerjee
  • 1,966
  • 3
  • 22
  • 28
  • 1
    possible duplicate of [UITabBar(Controller) - Get index of tapped?](http://stackoverflow.com/questions/3766230/uitabbarcontroller-get-index-of-tapped) – rishi Jun 27 '12 at 14:10

1 Answers1

0

you can use MyTabBar.selectedIndex and put in Object in AppDelegateClass.. Or if you have already TabBar Avaliable you can use

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

if you need to get it in the same calss so you can use

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

if you want to prevent user from going to another tab you can use delegate

-(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;
    }

}
Mina Nabil
  • 676
  • 6
  • 19
  • In the class that you want to get the selected tab in it.. just import your applicationdelegate class and use this code to access selected tabbar – Mina Nabil Jun 27 '12 at 14:10
  • But, buddy, is it possible, if I want to get it in the appdelegate class? Cause actually want user to prevent vising a view controller with respect to some condition. – Soumalya Banerjee Jun 27 '12 at 14:12
  • @SoumalyaBanerjee - i have linked a question above, you can check that. – rishi Jun 27 '12 at 14:17
  • 1
    I have edited my answer so you can prevent user from switching tabs based on condition.. – Mina Nabil Jun 27 '12 at 14:20