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.
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
UITabBarControlleris set by giving it anNSArrayofUIViewControllers or evenUINavigationControllers. The Tab Bar doesn’t mind which.A
UINavigationControlleris set by giving it aUIViewControllerthat 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.