-1

再会。我尝试将带有导航控制器的新选项卡添加到我的应用程序中。我创建了一个新的选项卡式应用程序(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;
}

导航控制器.h 文件

@interface NavController : UINavigationController

@end

这里是项目的结构 项目结构

当我运行项目时,它显示空选项卡 结果 但在 xib 文件中我添加标签和按钮 我想要的结果 可能是我忘记了什么?

4

2 回答 2

0

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

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

这可能会有所帮助。

于 2012-05-04T10:01:57.273 回答
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;
}

然后你会得到这个样子

在此处输入图像描述

在每个 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;

}
于 2012-05-04T10:05:47.587 回答