我使用更新标签的 NSTimer 编写了一个计时器。问题是,在同一个 View Controller 中,我有一个 uitableview,当我向下滚动它时,计时器不会更新其值,因此用户可以“作弊”来停止计时器。
我认为使用 CGD 的串行队列可以很容易地解决这个问题,但我不知道如何做到这一点。
提前致谢,
胡安
最佳答案
首先请记住,您不能在主线程以外的任何其他线程中执行 UI 更改。话虽如此,您需要在主队列中触发 NSTimer,否则在更改 UILabel 时程序会崩溃。看看这个链接http://bynomial.com/blog/?p=67还有这个http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html
据我所知,如果您在 NSRunLoopCommonModes
中安排计时器,它将忽略事件更新并按照您想要的方式触发计时器:
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:@selector(timerDidTick:)
userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
-(void) timerDidTick:(NSTimer*) theTimer{
[[self myLabel] setText:@"Timer ticked!"];
}
关于ios - 更新在后台运行的标签的计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16261620/