Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get tabbarcontroller from another view on my storyboard

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?

like image 386
Alosyius Avatar asked Jul 31 '13 12:07

Alosyius


2 Answers

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]; 
}
like image 66
Lucas Eduardo Avatar answered Nov 08 '22 23:11

Lucas Eduardo


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;
like image 25
Jiří Zahálka Avatar answered Nov 09 '22 00:11

Jiří Zahálka