TabBarController和NavigationController的问题

时间:2009-11-11 14:25:03

标签: iphone objective-c uinavigationcontroller uitabbarcontroller

我尝试以编程方式在每个UIViewController内部使用UINavigationController进行UITabBarController调用...尝试了所有内容...阅读每篇文章...但是我发现100%的教程都在UITableViewControllers中使用...这里是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

视图控制器的结构我希望显示导航栏:

//
//  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

我做错了什么?我知道如何使用IB这样做,但这次我需要以编程方式进行...提前致谢!

1 个答案:

答案 0 :(得分:6)

您的两个UINavigationController应该是UITabBarController的viewControllers。它们将显示您为其分配的rootViewController。

结构应该是这样的:

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

所以你的代码应该是:

- (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];
}