ios - 更新在后台运行的标签的计时器

标签 ios objective-c xcode nstimer

我使用更新标签的 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/

相关文章:

ios - Swift 中如何判断 MKPointAnnotation 是否被按下?

objective-c - UIScrollView 的 subview 中的水平 UISwipeGestureRecognizer? (UIScrollView 只需要识别垂直滚动)

iphone - 在指定的 UIView 中应用动画?

javascript - Youtube iFrame API PauseVideo 在 UIWebView 中未定义

ios - XCode swift HTTPS : String(data: data! 编码:NSUTF8StringEncoding ) 返回 nil

iphone - 命令/Developer/usr/bin/ibtool 失败,退出代码为 10

ios - UICollectionView 作为 UIViewController 的 subview

ios - NSMutableArray 是如何在快速枚举中达到这么高的速度的

ios - xcode 控制台中的 GKSConnSettings 消息

objective-c - 使用 SDWebImage 时构建失败