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

2

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;

}
Zelig answered 13/9, 2011 at 20:32 Comment(0)
N
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.

Nolie answered 13/9, 2011 at 20:55 Comment(1)
Hi, WOW both answers are right on. I'm going with the code route for now.Zelig
S
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.

Subprincipal answered 13/9, 2011 at 20:41 Comment(0)
N
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.

Nolie answered 13/9, 2011 at 20:55 Comment(1)
Hi, WOW both answers are right on. I'm going with the code route for now.Zelig

© 2022 - 2024 — McMap. All rights reserved.