3
votes

I'm working on a project that started out as a TabBarController-based app, and still mostly is, but some requirements have changed and we need to put in an initial view controller as an entry point to the rest of the app - one that will, after authentication is given and some things are loaded, will be replaced in the main window by the tab bar controller.

The original UITabBarController was a component of MainWindow.xib, and had all its root navigation controllers set up with root view controllers, and everything was going fine. I figured I could simply extract that UITabBarController component from the MainWindow.xib, put it in its own CustomTabBarController.xib, and then run

[self setTabBarController:[[[UITabBarController alloc] initWithNibName:@"CustomTabBarController" bundle:nil] autorelease]];
[_tabBarController setDelegate:self];
[self.window addSubview:_tabBarController.view];

However, instead of getting the pre-built tab bar controller I expected, I get a completely empty tab bar controller.

Am I missing something? Why can't I load a pre-built UITabBarController from a nib?

1

1 Answers

5
votes

Try this instead:

NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"CustomTabBarController" owner:nil options:nil];
UITabBarController *controller = [nibContents objectAtIndex:0];

[self setTabBarController:controller];
[_tabBarController setDelegate:self];
[self.window addSubview:_tabBarController.view];

It's kind of tricky to explain why, but it's to do with how file's owner works. If the tab bar controller is defined in the nib file, you can't load the nib file into a tab bar controller.

Nibs always have to be loaded by an object one level above the object defined in the nib - so an app delegate can load a view controller, a view controller can load a view, etc. But a view controller (including a TabBarController) can't load itself.