我似乎无法在TabBarItem
上添加徽章
尝试了很多选项(这就是代码被分成变量的原因)
认为它与踩踏有关,所以更新重新回到主线程,仍然没有。
代码:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSInteger badge_count = 0;
badge_count = getDataFromServer();
if (snacks_count > 0)
{
MainTabBarViewController *c = [self.storyboard instantiateViewControllerWithIdentifier:@"tabBarController"];
UINavigationController *nav = [c.viewControllers objectAtIndex:1];
dispatch_async(dispatch_get_main_queue(), ^{
nav.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", (long)badge_count];
});
}
});
答案 0 :(得分:0)
请注意方法:: instantiateViewControllerWithIdentifier"描述: 使用指定的标识符实例化并返回视图控制器。 您可以使用此方法创建要在应用程序中以编程方式操作和呈现的视图控制器对象。在使用此方法检索视图控制器之前,必须在Interface Builder中使用适当的标识符字符串对其进行显式标记。
即,您创建另一个实例。 你应该改为
UINavigationController *nav = [self.viewControllers objectAtIndex:1];
以便更改您引用的UINavigationController
答案 1 :(得分:0)
显然原来的问题是我没有从主线程中做到这一点。可能会在尝试调试的所有游戏中制作错误的代码。
工作代码:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
badge_count = getDataFromServer();
if (snacks_count > 0)
{
NSInteger badge_count = 0;
badge_count = getDataFromServer();
if (badge_count > 0)
{
dispatch_async(dispatch_get_main_queue(), ^{
[(UIViewController *)[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem].badgeValue = [NSString stringWithFormat:@"%ld", (long)badge_count];
});
}
});