R – Problems with TabBarController and NavigationController

iphoneobjective cuinavigationcontrolleruitabbarcontroller

im trying to programatically do a UITabBarController using a UINavigationController inside each UIViewController called by the tabs… tried everything… read every article… but 100% of the tutorials ive found uses in the UITableViewControllers… here is the app delegate:

//
//  mluThunderCamAppDelegate.m
//  mluThunderCam
//
//  Created by Paulo Ferreira on 11/9/09.
//  Copyright MobileLifeUtils.com 2009. All rights reserved.
//

#import "mluThunderCamAppDelegate.h"

@implementation mluThunderCamAppDelegate

@synthesize window, tbcMain;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    // Override point for customization after application launch
 UINavigationController *ncLocal;
 mluThunderCam *vcThunderCam = [[mluThunderCam alloc] init];
 ncLocal = [[UINavigationController alloc] initWithRootViewController:vcThunderCam];
 [ncLocal release];

 mluThunderCamPreferences *vcThunderCamPreferences = [[mluThunderCamPreferences alloc] init];
 ncLocal = [[UINavigationController alloc] initWithRootViewController:vcThunderCamPreferences];
 [ncLocal release];

 NSArray *aViewControllers = [[NSArray alloc] initWithObjects:vcThunderCam, vcThunderCamPreferences, nil];
 [vcThunderCam release];
 [vcThunderCamPreferences release];

 tbcMain = [[UITabBarController alloc] init];

 tbcMain.viewControllers = aViewControllers;

 [aViewControllers release];

    [window addSubview:tbcMain.view];
 [window makeKeyAndVisible];
}


- (void)dealloc {
    [window release];
    [super dealloc];
}


@end

And the structure of the view controller i want to have the navigation bar shown:

//
//  mluThunderCam.m
//  mluThunderCam
//
//  Created by Paulo Ferreira on 11/10/09.
//  Copyright 2009 MobileLifeUtils.com. All rights reserved.
//

#import "mluThunderCam.h"


@implementation mluThunderCam

-(id) init {
 self.title = @"Home";
 self.navigationItem.title = @"Damn!";
 return self;
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
 UIView *vwThunderCam = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
 self.view = vwThunderCam;
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

What am i doing wrong?? I know how to do this using the IB but this time i need to do it programmatically… Thanks in advance!

Best Answer

Your two UINavigationController should be the viewControllers of the UITabBarController. They will show up with the rootViewController you assigned them.

The structure should be like this:

UITabBarController
        |
        |____ UINavigationController
        |               |
        |          YourViewController
        |
        |____ UINavigationController
        |               |
       ...         AnotherViewController

So your code should be:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    // Override point for customization after application launch

    mluThunderCam *vcThunderCam = [[mluThunderCam alloc] init];
    UINavigationController *nav1; = [[UINavigationController alloc] initWithRootViewController:vcThunderCam];
    [vcThunderCam release];

    mluThunderCamPreferences *vcThunderCamPreferences = [[mluThunderCamPreferences alloc] init];
    UINavigationController *nav2; = [[UINavigationController alloc] initWithRootViewController:vcThunderCamPreferences];
    [vcThunderCamPreferences release];

    NSArray *aViewControllers = [[NSArray alloc] initWithObjects:nav1, nav2, nil];
    [nav1 release];
    [nav2 release];

    tbcMain = [[UITabBarController alloc] init];
    tbcMain.viewControllers = aViewControllers;
    [aViewControllers release];

    [window addSubview:tbcMain.view];
    [window makeKeyAndVisible];
}
Related Topic