bdunagan
fill the void

Posted
22 December 2009 @ 8am

Tagged
development

2 Comments

UITabBarController from a XIB

There are many good iPhone tutorials on creating a UITabBarController directly through code (a more complex implementation) or from the main XIB. However, I want to separate out the UITabBarController UI elements into a separate XIB and load them dynamically.

Most of the code is boilerplate. The key is creating the XIB in Interface Builder so that it hooks itself up when loaded. First, the code. The main controller needs to have an outlet for the tab class.

@interface RootViewController : UITableViewController {
	IBOutlet UITabBarController *tabBarController;
}
@property (nonatomic, retain) UITabBarController *tabBarController;

When the tab instance is needed, we can load it:

- (UITabBarController *)tabBarController {
	if (tabBarController == nil) {
		[[NSBundle mainBundle] loadNibNamed:@"TabBar" owner:self options:nil];
	}
	return tabBarController;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
	[tabBarController release];
	tabBarController = nil;
}

Next, the XIB. We change the “File’s Owner” object to the main controller class and connect the new tabBarController outlet to the UITabBarController in the XIB. When the XIB is loaded, the tab bar controller is added to the main controller.

I’ve posted an example project to Google Code. See the screenshot below.


2 Comments

Posted by
jan petersen
28 August 2010 @ 3am

Very cool tip and code indeed.
Only one thing: seems like the “TabBarController” in the example project is unused. Did you have any intention with this class?


Posted by
bdunagan
28 August 2010 @ 10am

@Jan Not really. Just demo’ing how to build the project quickly. :)


Leave a Comment