【发布时间】:2013-03-08 10:16:56
【问题描述】:
我有一个每 5 秒运行一次以检查新消息的功能,如果触发了新消息,我将使用以下代码更新标签栏项目的徽章值
NSString *chkUrl = @"http://domain.com/script.php";
NSURL *url = [[[NSURL alloc] initWithString:chkUrl] autorelease];
NSError *error = nil;
NSStringEncoding encoding;
NSString *returnHTML = [[NSString alloc] initWithContentsOfURL:url usedEncoding:&encoding error:&error];
int totalNewMessages = [[returnHTML stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] intValue];
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"badge before:%@",[[mainDelegate.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue);
[[mainDelegate.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue = [NSString stringWithFormat:@"%d",totalNewMessages];
NSLog(@"badge after:%@",[[mainDelegate.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue);
问题是徽章值没有立即更新!也许在第四次通话中的第三次,它会被更新!我做错了什么!还是 iOS 的 bug!
作为一种变通方法,我在每次更新时重复上述行 10 次,但它再次没有更新,所以问题是更新徽章值的延迟,这不是重新运行更新行的问题!
顺便说一句,这个问题发生在 Xcode 4.6 模拟器和我的 iPhone 5 和 iOS 6.1
【问题讨论】:
-
您为什么不设置断点来检查
totalNewMessages值,以便查看传递给您的徽章的内容?您可能有任何异步调用吗?首先如何获得价值? -
@Alladinian 我在更新之前和之后打印徽章值的值,它在 NSLog 中显示了正确的值,但它在标签栏项目徽章上延迟了几秒钟!跨度>
-
你能贴出你设置
totalNewMessages值的代码部分吗? -
@Alladinian 我已添加代码 :) 感谢您的跟进
标签: xcode ios6 uitabbaritem tabbarcontroller