在tabBarController中加载特定的View控制器

时间:2011-10-12 17:30:16

标签: iphone

我在tabBarController上加载特定的ViewControllers时遇到问题。这就是我的项目:

TabAppDelegate.m
--------------------

#import "TabAppDelegate.h"
#import "ViewControllerA.h"
#import "ViewControllerB.h"
//etc #import up to F 

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{
    tabBarController = [[UITabBarController alloc] init];

    UIViewController *viewController1 = [[[ViewControllerA alloc] initWithNibName:@"ViewControllerA" bundle:nil] autorelease];
    UIViewController *viewController2 = [[[ViewControllerA alloc] initWithNibName:@"ViewControllerB" bundle:nil] autorelease];
    tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];

    [window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
    return YES;
}


Interface builder
------------------
Tab Bar Controller
    ViewControllerA
    ViewControllerB
    ViewControllerC
    ViewControllerD
    ViewControllerE
    ViewControllerF

每个ViewController都链接到一个具有相同名称的类。

从上图中可以看出,在Interface Builder中我有一个标签栏控制器,里面有很多视图控制器。在applicationDidFinishLaunching中,我编写了一些(明显错误的!)代码,目的是在启动时仅在选项卡控制器中显示控制器A和B.但是,程序立即崩溃,并显示以下错误消息:

  

线程1:编程接收信号:“SIGABRT”

如果在applicationDidFinishLaunching中我省略了前4行代码(即,我不尝试仅加载ViewControllerA和ViewControllerB),程序将完美启动,但使用所有ViewControllers。

对我做错了什么的任何想法?谢谢!

1 个答案:

答案 0 :(得分:0)

如果已在Interface Builder中链接到tabBarController = [[UITabBarController alloc] init];,则不需要

UITabBarController

试试这个:

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{
    UIViewController *viewController1 = [[[ViewControllerA alloc] initWithNibName:@"ViewControllerA" bundle:nil] autorelease];
    UIViewController *viewController2 = [[[ViewControllerA alloc] initWithNibName:@"ViewControllerB" bundle:nil] autorelease];
    tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];

    [window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
    return YES;
}