Austin
Austin

Reputation: 4929

Using a navigationcontoller with a tabbar controller

I'm trying to make it so I have a tab bar at the bottom of my screen, and it's always there. I also want it so if I "click" into some menu in one of the tabs, it gives you the option to go back, thus a navigation controller and a tab bar controller.

I still don't really understand iOS, so the answer I found is confusing me.

This Answer: Having a UITabBar AND a UINavigationController in an app?

So how do I implement this? I'm guessing I change this method in my App Delegate,

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        viewController1 = [[CFSDFirstViewController alloc] initWithNibName:@"CFSDFirstViewController_iPhone" bundle:nil];
        viewController2 = [[CFSDSecondViewController alloc] initWithNibName:@"CFSDSecondViewController_iPhone" bundle:nil];
    } else {
        viewController1 = [[CFSDFirstViewController alloc] initWithNibName:@"CFSDFirstViewController_iPad" bundle:nil];
        viewController2 = [[CFSDSecondViewController alloc] initWithNibName:@"CFSDSecondViewController_iPad" bundle:nil];
    }
    self.tabBarController = [[UITabBarController alloc] init];
    [self.tabBarController setDelegate:self];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

Thanks for your assistance!

Upvotes: 0

Views: 432

Answers (1)

Lorenzo B
Lorenzo B

Reputation: 33428

First of all you don't need to to manually set up the nib name. You could just name them CFSDFirstViewController~iphone.xib and CFSDFirstViewController~ipad.xib. Then you can call [[CFSDFirstViewController alloc] init] and let iOS do the rest for you. For info see iOS Supports Device-Specific Resources.

About your question, you can only insert the UINavigationController within the UITabBarController. To do it wrap viewController1 within a UINavigationController like the following:

CFSDFirstViewController viewController1 = [[CFSDFirstViewController alloc] init];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:viewController1];

and then use navController instead of viewController1 like the folliwing

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

Check the code because I wrote by hand. And pay attention to memory if you use a non-ARC project.

Hope it helps.

Upvotes: 2

Related Questions