每x秒更新一次标签 [英] Update a label with speed every x seconds

查看:71
本文介绍了每x秒更新一次标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing my first iPhone application. I have to update a label with the device speed every x seconds. I have created my own CLController and I can get device speed but I don't know if I have got to use NSTimer to update my label. How can I do it?

推荐答案

您可以像这样安排计时器

You can schedule the timer like this

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

现在,下面的方法将在每个YOUR_INTERVAL(以秒为单位)内调用

Now below method will get called in every YOUR_INTERVAL (in seconds) periods

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

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

To stop the timer you could call invalidate on the timer object. So you might want to save the timer as a member variable, so that you can access it anywhere.

[timer invalidate];

这篇关于每x秒更新一次标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆