当我的应用加载时,我想根据用户上次运行应用时设置的保存设置更改其中一个标签上的图像。当用户单击执行该选项卡的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,但没有任何效果。我尝试在根视图控制器中创建IBOutLets到UITabBarItem和UINavigationController。
// tb is an iboutlet to the UITabBarItem
self.tb = tabItem;
// nc is an iboutlet to the UINavigationController
self.nc.tabBarItem = tabItem;
无济于事。知道怎么做吗?
答案 0 :(得分:2)
我明白了。我只需要调用UITabBarItem的setImage方法。
//UITabBarItem *filterTab = [self.tabBar.items objectAtIndex:2];
if (condition == YES) {
[[self.tabBar.items objectAtIndex:2] setImage:[UIImage imageNamed:@"filter plus.png"]];
// [filterTab setImage:[UIImage imageNamed:@"filter plus.png"]];
}
else {
[[self.tabBar.items objectAtIndex:2] setImage:[UIImage imageNamed:@"filter.png"]];
//[filterTab setImage:[UIImage imageNamed:@"filter.png"]];
}
跟进问题:首先将tabbaritem指定给指针然后设置图像是否有理由或优势?
UITabBarItem *filterTab = [self.tabBar.items objectAtIndex:2];
[filterTab setImage:[UIImage imageNamed:@"filter plus.png"]];
与在一行代码中执行如下操作?
[[self.tabBar.items objectAtIndex:2] setImage:[UIImage imageNamed:@"filter.png"]];
答案 1 :(得分:1)
我认为你需要尝试这个,希望这会有所帮助,
我更改了选定的tabbatitem图像,如 -
tabbar控制器委托方法中的
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([tabBarController selectedIndex] == 0)
{
[viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
}
}
通过这个你可以改变你的形象。
或者您可以直接在视图控制器init(或ViewWillAppear)方法中使用,例如
[viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
试试!!!!