【发布时间】:2016-11-28 10:13:41
【问题描述】:
在我的应用程序中,我有 NSTimer,它每秒更新一次 UILabel。我在NSTimer 之前添加了UIBackgroundTaskIdentifier,让它在后台运行。
__block UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:updateTimer forMode:NSRunLoopCommonModes];
。 . .
-(void)updateTime
{
secondsLeft -= 1;
//NSLog(@"Update time : %d ......",secondsLeft);
dispatch_async(dispatch_get_main_queue(), ^{
_timeLbl.text = [NSString stringWithFormat:@"%d",secondsLeft];
});
}
问题是当我们按下主页按钮应用程序进入后台时,计时器正在运行但uilabel 没有更新。例如。比如说,_timeLbl 显示“45”。现在,如果我按主页按钮,应用程序将进入后台。 10 秒后。我单击应用程序图标以在前台获取应用程序,然后我看到“45”一段时间(秒的分数),然后它显示 35,而不是直接显示 35。这意味着标签没有得到更新为NSTimer。有什么办法可以解决这个问题吗?
谢谢!
【问题讨论】:
-
更新主队列中标签的文本
-
如果你的意思是 dispatch_async(dispatch_get_main_queue(), ^{ ...... }); .......试过了,但没有运气
-
试试this
-
@Nikhil 我已经检查过了,但没有全部了解。您能否提供解决方案作为此链接和我的问题的参考?
标签: ios objective-c uilabel nstimer uibackgroundtask