在点击标签栏之前,不会显示tabBarItem

时间:2011-09-09 01:51:30

标签: iphone tabbarcontroller

我有一个TabBarController并在我的代码中定义了两个选项卡,如:

NSMutableArray *viewControllerArray = [[NSMutableArray alloc]initWithCapacity:2];

DisplayMarketViewController *displayMarketViewController = [[DisplayMarketViewController alloc]init];     
[viewControllerArray addObject:displayMarketViewController];
[displayMarketViewController release];

DisplayDifferenceMarketViewController *displayDifferenceMarketViewController = [[DisplayDifferenceMarketViewController alloc]init];
[viewControllerArray addObject:displayDifferenceMarketViewController];
[displayDifferenceMarketViewController release];
myTabBarController.viewControllers = viewControllerArray;

在我的DisplayMarketViewController.m

self.tabBarItem.title = @"Tab1";
UIImage *image = [UIImage imageNamed:@"1.png"];
self.tabBarItem.image =image;

和DisplayDifferenceMarketViewController.m

self.tabBarItem.title = @"Tab2";
UIImage *image = [UIImage imageNamed:@"2.png"];
self.tabBarItem.image =image;

但是在点击标签项之前不会显示Tab2。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在通过点击标签栏访问DisplayDifferenceMarketViewController之前,不会对其进行初始化。因此,在您访问选项卡之前,不会执行更改选项卡标题的代码。

只需将标签栏中所有与tabBar相关的代码放在视图控制器之外,其余的标签条形码如下:

NSMutableArray *viewControllerArray = [[NSMutableArray alloc]initWithCapacity:2];

DisplayMarketViewController *displayMarketViewController = [[DisplayMarketViewController alloc]init];     
[viewControllerArray addObject:displayMarketViewController];
[displayMarketViewController release];

DisplayDifferenceMarketViewController *displayDifferenceMarketViewController = [[DisplayDifferenceMarketViewController alloc]init];
[viewControllerArray addObject:displayDifferenceMarketViewController];
[displayDifferenceMarketViewController release];
myTabBarController.viewControllers = viewControllerArray;

[viewControllerArray objectAtIndex:0].tabBarItem.title = @"Tab1";
UIImage *image = [UIImage imageNamed:@"1.png"];
[viewControllerArray objectAtIndex:0].tabBarItem.image =image;

[viewControllerArray objectAtIndex:1].tabBarItem.title = @"Tab2";
UIImage *image = [UIImage imageNamed:@"2.png"];
[viewControllerArray objectAtIndex:1].tabBarItem.image =image;