-1

I'm playing with some sample code to try and figure out once and for all how to make navigation controller (s) and a tab controller to work together. As a bonus without memory leaks.

Having problems as shown below...

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

    // Override point for customization after application launch.
    UIViewController *viewController1 = [[[FirstViewController alloc] 
            initWithNibName:@"FirstViewController" bundle:nil] autorelease];
    UIViewController *viewController2 = [[[SecondViewController alloc] 
            initWithNibName:@"SecondViewController" bundle:nil] autorelease];

    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:
         viewController1, viewController2, nil];
    self.window.rootViewController = self.tabBarController;
    self.navigationController = [[UINavigationController alloc] 
         initWithRootViewController:self.tabBarController]; <<<<

    [self.window makeKeyAndVisible];
    return YES;

In my main project I have an outlet for 4 different navigation controllers and I call each like so.

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] 
      delegate];
[delegate.balNavController pushViewController:nextController animated:YES];

But this leaks and is causing problems.

Without using the interface builder, can someone advise me, in simple terms how I should be doing this, perhaps with a few lines of code.

3
  • You need to search for UITabBarController UINavigationController tutorials on Google or Youtube and learn everything and then try it out. The answer to this question is a learning process !
    – Legolas
    Dec 30, 2011 at 17:28
  • I have followed bad tutorials and bad examples. Hence, I'm asking a question to find out where I'm going wrong and what the correct / best approach !!
    – Jules
    Dec 30, 2011 at 17:32
  • You are doing it the wrong way around. You are putting your UITabBarController instance into the stack of a UINavigationController but instead you want to do it the other way around, judging from your question (which also would be the common way).
    – Till
    Dec 30, 2011 at 17:44

1 Answer 1

2

I strongly encourage your to pick up a copy of the Big Nerd Ranch Guide to iPhone Programming. It has excellent exposition and examples to get you proficient in the basics. Now on to your issues...

A UITabBarController is set by giving it an NSArray of UIViewControllers or even UINavigationControllers. The Tab Bar doesn't mind which.

A UINavigationController is set by giving it a UIViewController that will be the root.

Keep in mind that the Tab Bar is not a View Controller, so it cannot be the root of a Navigation Controller!

Combining these is simply a matter of correct order. Here's a generic example that hopefully illustrates this.

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{   
    // UIViewController with UINavigationBar
    UIViewController *firstViewController = [[UIViewController alloc] init];
    UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController:firstViewController];

    // Custom subclass of UIViewController with UINavigationBar
    CustomViewController *secondViewController = [[CustomViewController alloc] init];
    UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:secondViewController];

    // UIViewController without UINavigationBar
    UIViewController *thirdViewController = [[UIViewController alloc] init];

    // Set the NSArray of ViewControllers
    NSArray *viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, thirdViewController, nil];
    // This array retains the controllers, so go ahead and release them now
    [firstNavController release]; 
    [secondNavController release]; 
    [thirdViewController release]; 

    // Set up the UITabBarController -  It now holds everything
    tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:viewControllers];

    // Add the Tab Bar Controller to the window.
    [window addSubview:[tabBarController view]];
    [window makeKeyAndVisible];

    return YES;
}
2
  • 1
    Thanks for your answer. So I take it that firstNavController, secondNavController etc will be picked up as tab1, tab2 etc ? And that I can use self.navigationController through-out my app to push and pop view controllers / children of first and second view controllers, which will be maintained within each tab ?
    – Jules
    Dec 30, 2011 at 18:28
  • @Jules That's correct. Each tab has a separate nav controller (or none at all).
    – PengOne
    Dec 30, 2011 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.