3

我正在开发我的第一个 iPhone 应用程序。我必须每 x 秒用设备速度更新一个标签。我已经创建了自己的CLController,我可以获得设备速度,但我不知道我是否必须使用NSTimer来更新我的标签。我该怎么做?

4

2 回答 2

7

您可以像这样安排计时器

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:YOUR_INTERVAL 
                       target:self 
                       selector:@selector(updateLabel) 
                       userInfo:nil 
                       repeats:YES];

现在将在每个 YOUR_INTERVAL(以秒为单位)期间调用以下方法

- (void) updateLabel {
    myLabel.text = @"updated text";
}

要停止计时器,您可以在计时器对象上调用 invalidate。因此,您可能希望将计时器保存为成员变量,以便您可以在任何地方访问它。

[timer invalidate];
于 2011-03-23T10:58:27.013 回答
2

你是对的,你必须使用 NSTimer。您将在 x 秒后调用一种方法并更新标签。

[NSTimer scheduledTimerWithTimeInterval:x  target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];

-(void)updateLabel
{
    // update your label
}
于 2011-03-23T10:57:49.947 回答