【问题标题】:Reload / Refresh tab bar items in a ViewController ?在 ViewController 中重新加载/刷新选项卡栏项目?
【发布时间】:2010-11-18 15:04:45
【问题描述】:
我正在尝试在 ViewController 中更改标签栏的图像,但要显示新图像,我必须单击每个标签栏项目。
for (CustomTabBarItem *myItem in self.tabBarController.tabBar.items){
myItem.enabled = YES;
myItem.badgeValue = @"1";
UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:[[DesignManager sharedManager] getPathOfFile:@"test.png"]];
*myItem.imageSelect= *myImage; // change images of each item. don't appear if I dont click on the item
}
任何人都知道我怎样才能直接显示这些图像?
谢谢
【问题讨论】:
标签:
iphone
icons
uitabbarcontroller
uitabbar
uitabbaritem
【解决方案1】:
您需要将旧的标签栏项目替换为新的。否则您无法动态更新图像。
最简单的方法是设置由给定选项卡表示的视图控制器的 tabBarItem 属性。如果您想在该视图控制器中执行此操作,只需编写:
self.tabBarItem = [[UITabBarItem alloc] initWithTitle: @"title" image: myImage: tag: nil];
或者,您可以从其他地方执行此操作,例如您的应用委托:
UIViewController* vc = [tabBarController.viewControllers objectAtIndex: 3];
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle: @"title" image: myImage: tag: nil];
【解决方案2】:
我知道这是一个老问题。当我需要从另一个活动选项卡更新徽章值时,我遇到了同样的问题。创建另一个UITabBarItem 将解决您当前的问题,但在多次调用此代码时会导致潜在的内存泄漏。另外,当其他视图控制器访问选项卡时,它们没有引用新创建的UITabBarItem。我的诀窍是
vc.tabBarItem = vc.tabBarItem;
它对我有用。