Search code examples
iphonetabbarcontroller

No mainwindow.xib in Xcode 4 Confused how to have my TabBarController use a NavigationController


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;

}

Solution

  • 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.