简体   繁体   English

从我的故事板上的另一个视图中获取tabbarcontroller

[英]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.

相关问题 如何从一个故事板导航到另一个 TabBarController? - How to navigate from one storyboard to another TabBarController? hidebottombarwhenpush无法将TabbarController故事板移至另一个故事板 - hidebottombarwhenpush is not working in moving tabbarcontroller storyboard to another storyboard 如何在 swift 故事板中的另一个 TabBarController 中制作 TabBarController? - How to make TabBarController inside another TabBarController in swift storyboard? 当我从另一个视图选择回到视图时,TabBarController消失了 - TabBarController disappears when I segue back to the view from another view 引用从AppDelegate中的StoryBoard创建的TabbarController? - Referencing TabbarController created from StoryBoard in AppDelegate? ios将数据从TabbarController传递到Storyboard中的ViewController - ios Pass data from TabbarController to ViewController in Storyboard Xcode故事板:在TabBarController中显示自定义视图,而不是默认视图 - Xcode Storyboard: show custom view instead default view in TabBarController 从另一个情节提要中显示视图控制器 - Present view controller from another storyboard 从视图中访问tabBarController的属性? - Access properties of tabBarController from view? 使用Storyboard和TabBarController在视图控制器之间传输数据 - Transfer data between view controllers, using storyboard and TabBarController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM