Search code examples
ios5xcode4

Get selected tab in appdelegate


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.


Solution

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