在WindowBased app中更改TabBarItem文本

时间:2011-02-28 20:22:06

标签: objective-c cocoa-touch ios4 uitabbar uitabbaritem

我创建基于窗口的应用程序(称之为TabBar)。在TabBarAppDelegate中,我创建了两个UIViewControllers和一个UITabBarController。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UIViewController *vc1 = [[UIViewController alloc]
initWithNibName:@"MyFirstViewController" bundle:nil];

    UIViewController *vc2 = [[UIViewController alloc]
initWithNibName:@"MySecondViewController" bundle:nil];

    NSArray *contr = [NSArray arrayWithObjects:vc1, vc2, nil];
    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = contr;
    [self.window addSubview:tbc.view];
    [self.window makeKeyAndVisible];
    [vc1 release]; [vc2 release];
    return YES;
}

在MyFirstViewController.xib中,我创建了uilabel(使用IB进行测试以加载视图)和MySecondViewController.xib。

应用程序构建并运行成功,但uitabbaritem - 没有图像和标题。我尝试在MyFirstViewController.m中添加这些行来设置MyFirstViewController的标题和图像

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UITabBarItem *item = [[UITabBarItem alloc] 
initWithTitle:@"first" image:[UIImage imageNamed:@"first.png"] tag:0];

        self.tabBarItem = item;
        [item release];
    }
    return self;
}

但是第一个tabbaritem并没有改变。请帮帮我。我不明白我做错了什么。

1 个答案:

答案 0 :(得分:0)

答案是

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    MyFirstViewController *vc1 = [[MyFirstViewController alloc]
initWithNibName:@"MyFirstViewController" bundle:nil];

    MySecondViewController *vc2 = [[MySecondViewController alloc]
initWithNibName:@"MySecondViewController" bundle:nil];

    NSArray *contr = [NSArray arrayWithObjects:vc1, vc2, nil];
    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = contr;
    [self.window addSubview:tbc.view];
    [self.window makeKeyAndVisible];
    [vc1 release]; [vc2 release];
    return YES;
}