5

This was pretty easy in Xcode 3. But I'm totally lost in Xcode 4.* It looks like IB is not used at all. And all the TabBarController code is in code.

Question: How do I add a NavigationBarController to the default code that Xcode generates when using a TabBarController template?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.

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

UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

self.tabBarController = [[UITabBarController alloc] init];

self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];

self.window.rootViewController = self.tabBarController;

[self.window makeKeyAndVisible];

return YES;

}
Edward Potter
  • 3,760
  • 4
  • 28
  • 39

2 Answers2

6

You can add a MainWindow.xib file manually (New File -> Empty interface builder document) and then in your apps Info.plist you can add a key called "Main nib file base name" and set it's value to "MainWindow".

In your app delegate, set the window and the UINavigationController as IBOutlets and remove the code that generates them. Then in your MainWindow.xib file add an instance of the app delegate, the UINavigationController, and a Window. Connect the UINavigationController and Window to the outlets of the delegate.

Michael Frederick
  • 16,664
  • 3
  • 43
  • 58
6

As someone has mention you can add a xib file an configure the app to use it. Here is the code version in case you decide to go this route it's always best to know either way

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UIViewController *viewController1 = [[FirstViewController alloc] init];
    UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
    [viewController1 release]; viewController1 = nil;

    UIViewController *viewController2 = [[SecondViewController alloc] init];
    UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
    [viewController2 release]; viewController2 = nil;

    self.tabBarController = [[UITabBarController alloc] init];

    NSArray *viewController = [[NSArray alloc] initWithObjects:navigationController1, navigationController2, nil];
    [navigationController1 release]; navigationController1 = nil;
    [navigationController2 release]; navigationController2 = nil;

    self.tabBarController.viewControllers = viewControllers;
    [viewControllers release]; viewControllers = nil;

    self.window.rootViewController = self.tabBarController;

    [self.window makeKeyAndVisible];

    return YES;
}

This is written in the browser but it should work.

Paul.s
  • 38,494
  • 5
  • 70
  • 88