【发布时间】:2026-02-19 23:50:02
【问题描述】:
当我的应用加载时,我想根据用户上次运行应用时设置的保存设置更改其中一个选项卡上的图像。当用户单击执行该选项卡的 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;
无济于事。知道怎么做吗?
【问题讨论】:
标签: iphone uitabbaritem