简体   繁体   中英

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 ?

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM