开发者

tabBarItem isn't displayed until the tab bar is tapped

开发者 https://www.devze.com 2023-04-03 12:18 出处:网络
I have a TabBarController and define two tabs in my code like: NSMutableArray *viewControllerArray = [[NSMutableArray alloc]initWithCap开发者_如何学Cacity:2];

I have a TabBarController and define two tabs in my code like:

NSMutableArray *viewControllerArray = [[NSMutableArray alloc]initWithCap开发者_如何学Cacity:2];

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

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

in my DisplayMarketViewController.m

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

and DisplayDifferenceMarketViewController.m

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

but the Tab2 isn't show until the tab item is tapped. How do I solve this problem?


Your DisplayDifferenceMarketViewController isn't initialized until it's accessed by tapping the tab bar. Therefore, the code to change the tab title isn't executed until you access it.

Simply put all of your tabBar related code outside the view controllers in the tab bar, with the rest of your tab bar code like so:

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消