Tab bar controller inside a navigation controller, how to push new viewcontrollers to the tabcontroller?

和自甴很熟 提交于 2019-12-06 15:54:51

问题


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!



来源:https://stackoverflow.com/questions/11136283/tab-bar-controller-inside-a-navigation-controller-how-to-push-new-viewcontrolle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!