问题描述
在我的代表中,我试图选择我的 TabBarController
,以便我可以使用不同的背景设置它。但问题是我的 TabBarController
不在rootView上..
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..
我当前的代码:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
在我的界面构建器中,我有 TabBarController
使用Segue名称设置:mainView(这是 TabBarController
所在的位置)。
In my interface builder i have my TabBarController
setup with a Segue name: mainView (This is where the TabBarController
is located).
我如何选择我的 TabBarController
?
推荐答案
首先,您必须在视图层次结构中知道TabBarController的位置。如果它不是你的根控制器,找到调用TabBarController的UIViewController,并通过segue或类似的东西获得它的引用。
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.
什么可能对你有用,它正在访问tabViewController中选项卡中第一个子UIViewController的 viewDidLoad
中的 tabBarController
属性。 tabBarController的所有子ViewControllers都有这个属性。
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.
例如,假设tabBar中显示的第一个UIViewController是MyViewController,执行如下所示的操作:
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];
}
这篇关于从我的故事板上的另一个视图中获取tabbarcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!