Get tabbarcontroller from another view on my storyboard

17,332

Solution 1

First, you have to know in your view hierarchy where is your TabBarController. If it's not your root controller, Locate the UIViewController that are calling the TabBarController, and get it's reference by segue or something like it.

What might work for you, it's accessing the tabBarController property in the viewDidLoad of the first child UIViewController in a tab inside your tabViewController. All child ViewControllers of the tabBarController have this property.

For example, assuming first UIViewController displayed in the tabBar is MyViewController, perform something like this:

- (void)viewDidLoad
{
   UITabBar *tabBar = self.tabBarController.tabBar;
   UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
   UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1]; 
}

Solution 2

If you want to get it from ONE OF the VIEWS

//if Custom class
TabBarController *tabBar = (TabBarController *) self.tabBarController;

//if Custom class with Navigation Controller
TabBarController *tabBar = (TabBarController *) self.navigationController.tabBarController;

//if Not Subclassed
UITabBarController *tabBar = (UITabBarController *) self.tabBarController;

//if Not Subclassed with Navigation Controller
UITabBarController *tabBar = (UITabBarController *) self.navigationController.tabBarController;
Share:
17,332
Alosyius
Author by

Alosyius

Updated on June 15, 2022

Comments

  • Alosyius
    Alosyius almost 2 years

    In my Delegate i am trying to select my TabBarController so that i can style it with a different background. However the problem is that my TabBarController is not located on the rootView..

    My current code:

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
    

    In my interface builder i have my TabBarController setup with a Segue name: mainView (This is where the TabBarController is located).

    How could i select my TabBarController?