[英]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. 在我的代表中,我正在尝试选择我的
TabBarController
以便我可以使用不同的背景设置它。 However the problem is that my TabBarController
is not located on the rootView.. 但问题是我的
TabBarController
不在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). 在我的界面构建器中,我的
TabBarController
设置有一个Segue名称:mainView(这是TabBarController
所在的位置)。
How could i select my TabBarController
? 我怎么能选择我的
TabBarController
?
First, you have to know in your view hierarchy where is your TabBarController. 首先,您必须在视图层次结构中知道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.
如果它不是您的根控制器,请找到调用TabBarController的UIViewController,并通过segue或类似的东西获取它的引用。
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. 什么可能为你工作,它的访问
tabBarController
在属性viewDidLoad
你tabViewController内标签的第一个孩子的UIViewController。 All child ViewControllers of the tabBarController have this property. tabBarController的所有子ViewControllers都具有此属性。
For example, assuming first UIViewController displayed in the tabBar is MyViewController, perform something like this: 例如,假设tabBar中显示的第一个UIViewController是MyViewController,执行如下操作:
- (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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.