Get tabbarcontroller from another view on my storyboard

我只是一个虾纸丫 提交于 2019-12-12 07:59:48

问题


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?


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



回答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;


来源:https://stackoverflow.com/questions/17970004/get-tabbarcontroller-from-another-view-on-my-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!