fill the void - bdunagan

22 Dec 2009
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.

Previous LinkedIn GitHub Email Next