7

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?

4

2 回答 2

11

首先,您必须知道您的视图层次结构中的 TabBarController 在哪里。如果它不是您的根控制器,请找到正在调用 TabBarController 的 UIViewController,并通过 segue 或类似的方式获取它的引用。

什么可能对您有用,它正在访问您的 tabViewController 内的选项卡中的第一个子 UIViewController 的属性tabBarControllertabBarController 的所有子 ViewController 都有这个属性。viewDidLoad

例如,假设 tabBar 中显示的第一个 UIViewController 是 MyViewController,执行如下操作:

- (void)viewDidLoad
{
   UITabBar *tabBar = self.tabBarController.tabBar;
   UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
   UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1]; 
}
于 2013-07-31T12:28:26.100 回答
7

如果您想从其中一个视图中获取它

//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;
于 2014-09-11T22:16:07.703 回答