当我的应用程序加载时,我想根据用户上次运行应用程序时设置的保存设置更改其中一个选项卡上的图像。当用户单击执行该选项卡的 viewcontroller 的 ViewDidLoad 方法的选项卡时,我可以更改图像。见下文:
UITabBarItem *tabItem;
if (condition = YES) {
tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter plus.png"] tag:self.view.tag];
}
else {
tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter.png"] tag:self.view.tag];
}
self.navigationController.tabBarItem = tabItem;
[tabItem release];
[super viewDidLoad];
但是我一直无法弄清楚如何在加载时在应用程序的根视图控制器中访问和更改该选项卡的 UITabBarItem。请参阅下面的根视图控制器的 viewdidload 方法。
UITabBarItem *tabItem;
if (condition = YES) {
tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter plus.png"] tag:self.view.tag];
}
else {
tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter.png"] tag:self.view.tag];
}
// get the view controller of the tab I want to change
MyViewController *vc = [self.tabBarController.viewControllers objectAtIndex:2];
ft.tabBarItem = tabItem;
[tabItem release];
[super viewDidLoad];
当这不起作用时,我尝试了多种其他方法来访问和更改 uitabbaritem 但没有任何效果。我尝试在 UITabBarItem 和 UINavigationController 的根视图控制器中创建 IBOutLets。
// tb is an iboutlet to the UITabBarItem
self.tb = tabItem;
// nc is an iboutlet to the UINavigationController
self.nc.tabBarItem = tabItem;
无济于事。知道怎么做吗?