在 ViewController 中重新加载/刷新选项卡栏项目?

发布于 2024-10-03 16:45:12 字数 495 浏览 5 评论 0原文

我正在尝试更改 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
}

有人知道如何直接显示这些图像吗? 谢谢

I am trying to change images of my tabbar in a ViewController, but to display the new images, I must click on each tab bar item.

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
}

Anyone know How can I can display directly these images?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我纯我任性 2024-10-10 16:45:12

我知道这是一个老问题。当我需要从另一个活动选项卡更新徽章值时,我遇到了同样的问题。创建另一个 UITabBarItem 将解决您当前的问题,但在多次调用此代码时会导致潜在的内存泄漏。另外,当其他视图控制器访问该选项卡时,它们不会引用新创建的 UITabBarItem。我的技巧是

vc.tabBarItem = vc.tabBarItem; 

它对我有用。

I know this is an old question. I ran into the same problem when I need to update the badge value from another active tab. Creating another UITabBarItem will solve your current problem but causes potential memory leak when this code is called many times. Plus, when other view controllers access the tab, they do not have reference to newly created UITabBarItem. My trick is

vc.tabBarItem = vc.tabBarItem; 

It works for me.

深者入戏 2024-10-10 16:45:12

您需要用新选项卡栏项目替换旧选项卡栏项目。否则您无法动态更新图像。

最简单的方法是设置给定选项卡表示的视图控制器的 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];

You need to replace the old tab bar item with a new one. You can't update the image dynamically otherwise.

The easiest way to do this is to set the tabBarItem property of the view-controller represented by a given tab. If you wanted to do this from within that view controller, just write:

self.tabBarItem = [[UITabBarItem alloc] initWithTitle: @"title" image: myImage: tag: nil];

Or, you could do this from somewhere else, say your app delegate:

UIViewController* vc = [tabBarController.viewControllers objectAtIndex: 3];
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle: @"title" image: myImage: tag: nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文