1
votes

I have a root UINavigationController and want to init it with an instance of UITabBarController, something like this:

TabBarController * viewController = [[TabBarController alloc] init];
UINavigationController navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];

As per the documentation for initWithRootViewController: method, this is a bad idea:

The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.

So I wonder:

  1. Why do we have such a restriction?
  2. ...why does it work and doesn't throw any exceptions? Are there any side effects of such approach? I'm trying to reuse the single navigation controller across all tabs of my tab bar controller and so far the code from above works quite well.

What I need is to 1) have a consistent NavBar in all of my tabs (but with different titles and left/right icons 2) Some tabs must support drill-down navigation 3) I don't need the tab bar panel when switching to deeper elements of the screens hierarchy.

EDIT

I've just realized that Skype for iOS is a good example of what I am trying to achieve: it works exactly like my app in terms of tabs and navigation.

1
I don't know the technical reason but it strikes me that it would lead to a confusing user experience; By having a tab controller at the root of your navigation controller, the user must navigate all the way back to the root in order to switch tabs; The general use case for a tab bar controller would see you embed a separate navigation controller in each tab, then the user can rapidly switch between tabs without losing their place within each tab's stack of viewsPaulw11
@Paulw11 thank you for your comment. Currently I am using a single nav controller for all of my tabs. I've seen similar recommendations to embed each each tab's UIViewController into UINavigationController, but... why do I need that? I understand the conceptual idea behind this approach, but cannot realize why to have 5 nav controllers instead of just one... What I need is to 1) have consistent NavBar in all of my tabs 2) Some tabs has drill-down navigation.fraggjkee
As I said, having a navigation controlller per tab allows the user to quickly switch between tabs without having to navigate back to the root and to maintain their hierarchical position in each tab. It's not like it is very difficult to create 5 navigation controllersPaulw11
Ah, I've finally got your idea, thanks. It really makes sense to have multiple nav controllers in this case then. But in my particular case I don't need it - when user goes deeper from one of the tabs I show subsequent controllers fullscreen, no need to keep the tab bar panel.fraggjkee
Then I would question using a tab controller as the root; your use is non-standard and could be confusing. Perhaps your root view could simply be a list of options like a menu and each entry just pushes a new view controllerPaulw11

1 Answers

3
votes

I suppose the reason is that if you push another view controller onto the navigation stack, it would push the tab bar controller to the side in a standard animation, resulting in a potentially confusing user experience.

As you switch to different controller with the tab bar, you would be pushing the next view controller on top of different controllers, at least from the perspective of the user - this can be quite perplexing.

The tab bar is really meant to be always there, so it does not make much sense to embed it into another controller of controllers. You can hide the bottom bar for specific screens if necessary.

The fact that ultimately it is just a controller of controllers is also the reason why it works. The controller of controllers is oblivious about what kind of controllers it controls.

If you describe your exact use case, you be able to get more specific advice if this is a good idea in your case or not. Perhaps you might want to consider using a UIToolbar instead.