简体   繁体   English

无法从选项卡栏控制器访问导航栏

[英]Navigation Bar not accessible from Tab Bar controller

Shown in the image below is a setup that I'm trying to create with the following hierarchy: 下图显示了我正在尝试使用以下层次结构创建的设置:

  1. Root Single View Controller (from left, second view in screenshot) embedded into a Navigation Controller (from left, first view in screenshot). 根单一视图控制器(在屏幕截图中从左,第二个视图)嵌入到导航控制器中(在屏幕快照中,从左第一个视图)。
  2. Tab Bar Controller (from left, third view in screenshot that is accessed from the root vc 标签栏控制器(从屏幕左侧的第三个视图中,可从根vc访问
  3. 2 View Controllers (the 2 most right views in screenshot) that are embedded in the tab bar controller and seemingly also still in the navigation bar controller. 2个视图控制器(屏幕快照中最右边的2个视图)嵌入在标签栏控制器中,并且似乎仍在导航栏控制器中。

Each Tab Bar Controller shows a navigation bar (as you can see in the right 2 views). 每个选项卡栏控制器都显示一个导航栏(如您在右2个视图中看到的)。 The logic that does work is that when I press the button on the root, the tab bar controller opens. 起作用的逻辑是,当我按下根目录上的按钮时,选项卡栏控制器将打开。 You can navigate between tabs via the tab bar and you get back to the root by pressing the back button in the navigation bar. 您可以通过选项卡栏在选项卡之间导航,然后通过按导航栏中的“后退”按钮返回到根目录。

What I don't understand is how I can access the navigation bar on each of the right most views. 我不了解的是如何访问最正确的每个视图上的导航栏。 As mentioned above, the navigation bar shows the back button, but I can't access the title, or add any navigation bar buttons. 如上所述,导航栏显示后退按钮,但我无法访问标题或添加任何导航栏按钮。 When I do, nothing is displayed (except for the back button). 当我这样做时,什么都没有显示(“后退”按钮除外)。 So What I want is to be able to set the title of the navigation bar in each view controller that is connected to the tab bar controller. 因此,我想要的是能够在连接到标签栏控制器的每个视图控制器中设置导航栏的标题。 And as shown in the screenshot below, there clearly is a navigation bar which comes from embedded root view controller. 如下面的屏幕快照所示,显然有一个导航栏,该导航栏来自嵌入式根视图控制器。

Some more info that may make things clearer: 一些更多的信息可以使事情变得更清楚:

  • I have relationships from the tab bar controller to each view controller that the tab bar manages. 我从标签栏控制器到标签栏管理的每个视图控制器都有关系。
  • I tried embedding the right most view controllers into navigation controllers, but that still didn't work. 我尝试将最合适的视图控制器嵌入导航控制器,但这仍然行不通。
  • I tried to add a navigation bar item to the view controllers manually and couldn't get access to the title either. 我尝试将导航栏项手动添加到视图控制器,但也无法访问标题。

Any Ideas What I'm Doing Wrong ?? 任何想法,我在做什么错了? Thanks! 谢谢!

标签/导航控制器

I found a way to do it: 我找到了一种方法:

In each view controller managed by the tab bar controller, I add this to the viewWillAppear method: 在选项卡栏控制器管理的每个视图控制器中,我将其添加到viewWillAppear方法中:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    PHOrderTabBarViewController *orderTabBarViewController = (PHOrderTabBarViewController *)self.tabBarController;
    orderTabBarViewController.title = @"Contacts";
    orderTabBarViewController.navigationItem.rightBarButtonItem = nil; // if no button is used
}

PHOrderTabBarViewController is the TabBarViewController that manages the view controllers on the right side of the screenshot. PHOrderTabBarViewController是TabBarViewController,用于管理屏幕截图右侧的视图控制器。 Since the navigation controller is embedded in the TabBarViewController, I have to set the title and any buttons from there. 由于导航控制器嵌入在TabBarViewController中,因此我必须从那里设置标题和所有按钮。

Note!! 注意!! : When navigating between tabs, the above needs to be added to each view controller managed by the tab bar controller. :在选项卡之间导航时,需要将以上内容添加到由选项卡栏控制器管理的每个视图控制器中。 Otherwise, the navigation bar items will carry over to the other views. 否则,导航栏项目将转移到其他视图。 (Eg in my code, the navigation title is set to 'Contacts', if I would press on the other tab, it would still display 'Contacts' unless I reset the title/button in that view's viewWillAppear method. (例如,在我的代码中,导航标题设置为“联系人”,如果我按另一个选项卡,除非我在该视图的viewWillAppear方法中重置标题/按钮,否则它仍将显示“联系人”。

With this result, I'm not sure if this is the right behavior or pattern to use here. 有了这个结果,我不确定这是否是正确的行为或模式。 The app now works as expected, but I don't know if I will run into issues with navigation if any of the tabbed bar controllers need to navigate into deeper hierarchies. 该应用程序现在可以按预期运行,但是我不知道是否有任何选项卡式条形控制器需要导航到更深的层次结构时是否会遇到导航问题。 Also, I haven't tried what @AmroShafie has suggested, but if I do, I will come back to note the outcome. 另外,我还没有尝试@AmroShafie的建议,但是如果我这样做了,我会回来注意结果。

In your viewDidLoad method in each view controller you should do this (changing the title from @"Contacts" as appropriate: 在每个视图控制器的viewDidLoad方法中,您应该执行此操作(根据需要更改@“ Contacts”的标题:

self.title = @"Contacts";
self.navigationItem.rightBarButtonItem = nil; // if no button is used

The view controller is responsible for its own navigation bar. 视图控制器负责其自己的导航栏。 In your AppDelegate you can create the tab bar controller like this: 在您的AppDelegate中,您可以创建标签栏控制器,如下所示:

// Create a tabbar controller and an array to contain the view controllers
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;

NSMutableArray *localViewControllersArray   = [[NSMutableArray alloc] init];

//  Setup first view and nav controllers
UIViewController *vc1 = [[UIViewController alloc] init];
UINavigationController *nc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
[localViewControllersArray addObject:nc1];

//  Setup second view and nav controllers
UIViewController *vc2 = [[UIViewController alloc] init];
UINavigationController *nc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
[localViewControllersArray addObject:nc2];

// set the tab bar controller view controller array to the localViewControllersArray
tabBarController.viewControllers = localViewControllersArray;

[self.window setRootViewController:tabBarController];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM