Tab bar controller inside a navigation controller,

2020-08-01 07:01发布

问题:

Design requirement:

  1. Show a list of items the user can pick from
  2. After having picked an item, bring the user to a new view with a back button. The new view should contain a list of tabs at the bottom that are not present in the first screen
  3. When clicking an item in the tabs, a new screen should appear with a back button and the tabs should still be visible at the bottom.
  4. Clicking a tab should take the user back up the hierarchy to #2. Not to the first screen.

I have tried following structure:

  • UINavigationController
  • UIViewController with a UITableView
  • UIViewController with a UITabBar (like here http://www.wiredbob.com/2009/04/iphone-tweetie-style-navigation.html)

and also

  • UINavigationController
  • UIViewController with a UITableView
  • UITabbarController

Both cases work fine with displaying the UITabBar, but when I click an item in one of the tabs and push a new UIViewController, then the tabs at the bottom disappears. I want the tabs to remain in place for all pushed UIViewControllers that occurs inside a tab of the UITabBarController.

A related question is this one but it doesn't deal with the problem of pushed viewcontrollers inside a tab: Tab bar controller inside a navigation controller, or sharing a navigation root view

Do I need to change the rootcontroller to the UITabController? Anyone actually implemented this?

回答1:

Here is the correct structure:

UITabBarcontroller (UIWindow's rootViewController)
->UINavigationController (first tab)
-->UIViewController
->UINavigationController (second tab)
-->UIViewController


回答2:

It sounds like you want to change the layout of your view hierarchy to accommodate your requirements. You should present your view controllers as such:

UITabBarController -> UINavigationController -> UIViewController

In your app delegate, you can implement this programmatically using something along the lines of:

UIViewController *viewControllerOne = [[[UIViewController alloc] init] autorelease];
UINavigationController *navigationControllerOne = [[[UINavigationController alloc] initWithRootViewController:viewControllerOne] autorelease];

UIViewController *viewControllerTwo = [[[UIViewController alloc] init] autorelease];
UINavigationController *navigationControllerTwo = [[[UINavigationController alloc] initWithRootViewController:viewControllerTwo] autorelease];

UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];

[tabBarController setViewControllers:[NSArray arrayWithObjects:navigationControllerOne, navigationControllerTwo, nil]];

[[self window] setRootViewController:tabBarController]

I haven't checked the above, it's just written from memory but should do what you require as an example.

Using this format, you can push any additional view controllers on to the navigation controller stack without your tab bar disappearing.

If you want to push this view hierarchy without having the tab bar controller as your root view controller, simply push the tab bar controller instead of setting it as the root view controller in the app delegate.

Hope that helps!