0

我有一个 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 不会显示。我该如何解决这个问题?

4

1 回答 1

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;
于 2011-09-09T01:57:00.803 回答