将nav控制器添加到选项卡式应用程序

时间:2012-05-04 09:50:54

标签: iphone ios xcode4.2

美好的一天。我尝试将带导航控制器的新标签添加到我的应用程序中。我创建了一个新的选项卡式应用程序(xcode 4.2),并在appdelegate中编写此

- (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];
    NavController *navController = [[[NavController alloc] initWithNibName:@"NavController" bundle:nil] autorelease]; //my controller
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, navController, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

NavController.h文件

@interface NavController : UINavigationController

@end

这里是项目的结构 Structure of progect

当我运行项目时,它会向我显示空标签 result 但是在xib文件中我添加了标签和按钮 What I want in result 可能是我忘记了什么?

2 个答案:

答案 0 :(得分:0)

使用以下的一些UIViewController初始化导航控制器:

    RootViewController *rootViewController = [[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease];
    NavController *navController = [[[NavController alloc] initWithRootViewController: rootViewController] autorelease]; 

这可能会有所帮助。

答案 1 :(得分:0)

我正在分享您关于此功能的APP代码,希望这可以轻松解决您的问题

在AppDelegate.m

中添加此代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

    UINavigationController *localNavigationController;

    tabBarController = [[UITabBarController alloc] init];

    NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:3];

    viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [localControllersArray addObject:localNavigationController];

    AlaramClock *aAlaramClock = [[[AlaramClock alloc] initWithNibName:@"AlaramClock" bundle:nil] autorelease];
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:aAlaramClock];
    [localControllersArray addObject:localNavigationController];


    CurrentTime *aCurrentTime = [[[CurrentTime alloc] initWithNibName:@"CurrentTime" bundle:nil] autorelease];
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:aCurrentTime];
    [localControllersArray addObject:localNavigationController];

    Settings *aSettings = [[[Settings alloc] initWithNibName:@"Settings" bundle:nil] autorelease];
    localNavigationController = [[UINavigationController alloc] initWithRootViewController:aSettings];
    [localControllersArray addObject:localNavigationController];



    tabBarController.viewControllers = localControllersArray;

    [localControllersArray release];


    // Override point for customization after app launch    
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];

    return YES;
}

然后你会看到这个

enter image description here

在每个ViewController.m

之后

添加此

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) 
    {
        // Custom initialization
        self.title=@"Calculate Time";
        self.tabBarItem.title = @"Calculate Time";
        self.tabBarItem.image=[UIImage imageNamed:@"time_calculate.png"];
    }

    return self;

}