9

我有一个 NSTimer

timer = [NSTimer scheduledTimerWithTimeInterval:1 
                                         target:self 
                                       selector:@selector(periodicTimer) 
                                       userInfo:nil 
                                        repeats:YES];

哪个

- (void)periodicTimer
{
    NSLog(@"Bang!");
    if (timerStart != nil)
        [timerLabel setText:[[NSDate date] timeDifference:timerStart]];        
}

问题在于,在滚动表格视图(或执行其他任务)时,标签不会更新,此外,“砰!” 没有出现,所以我认为该方法不会被调用。

我的问题是即使用户正在使用应用程序界面,如何定期更新标签。

4

2 回答 2

27

您需要将计时器添加到 UITrackingRunLoopMode 以确保您的计时器在滚动期间也会触发。

NSRunLoop *runloop = [NSRunLoop currentRunLoop];
NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(myTimerAction:) userInfo:nil repeats:YES];
[runloop addTimer:timer forMode:NSRunLoopCommonModes];
[runloop addTimer:timer forMode:UITrackingRunLoopMode];

来自: https ://stackoverflow.com/a/1997018/474896

于 2012-05-26T11:58:19.353 回答
-2

不确定这个,但我的第一个猜测是,渲染界面的主线程在更新界面时没有机会做任何事情。

您可以为您的计时器创建一个带有新运行循环的新线程,但这可能是一个丑陋的解决方案。你想在你的应用程序中实现什么功能?也许我们可以提出比使用计时器更好的策略。

于 2012-05-26T10:02:30.823 回答