Get tabbarcontroller from another view on my storyboard

As 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 not in the root directory.

My current code is:

 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 constructor, I have my TabBarController installation named Segue: mainView (here is the TabBarController ).

How can I choose a TabBarController ?

+6
source share
2 answers

First, you should know in your view hierarchy where your TabBarController is. If this is not your root controller, find the UIViewController that invokes the TabBarController and get its link via segue or something like that.

Which may work for you, it accesses the tabBarController property in viewDidLoad first child UIViewController on the tab inside your tabViewController. All child ViewControllers tabBarController have this property.

For example, if the first UIViewController displayed in the tabBar is MyViewController, do the following:

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

If you want to get it from ONE OF 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; 
+5
source

Source: https://habr.com/ru/post/950713/


All Articles