0

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:

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?

Community
  • 1
  • 1
Christer Nordvik
  • 2,518
  • 3
  • 35
  • 52

2 Answers2

3

Here is the correct structure:

UITabBarcontroller (UIWindow's rootViewController)
->UINavigationController (first tab)
-->UIViewController
->UINavigationController (second tab)
-->UIViewController
graver
  • 15,183
  • 4
  • 46
  • 62
  • But I don't want the tabs in the first screen. My first screen is a list of items (like the accounts in Tweetie). When clicking an item I push a new controller that shows the tabs. That's the main issue as it's a bit "unstandard" but Tweetie did it back when it was available in the AppStore. – Christer Nordvik Jun 21 '12 at 10:41
  • It is the same. You just have to create programmatically your UITabBarController, then initialize UINavigationController with rootViewController for each nav. controller (which are your actual controllers), and set `viewControllers` property of the tab bar controller with an array of the navigation controllers you have created... – graver Jun 21 '12 at 10:53
  • 1
    Oh, I am not sure how I missed that one. Thanks for pointing it out! I now have two navheaders but at least it works. I'll just pull some magic tricks to show the right one :-) – Christer Nordvik Jun 21 '12 at 11:42
3

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!

Zack Brown
  • 5,990
  • 2
  • 41
  • 54
  • Thanks! I updated the question as I saw that it was a bit unclear. I don't want tabs in the first screen, so can I switch rootController back and forth between a tabController and a navigationController? – Christer Nordvik Jun 21 '12 at 10:48
  • Yes, that is possible. Or you can push the tab bar controller from your original view controller rather than changing the root view controller. – Zack Brown Jun 21 '12 at 10:53