Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以每x秒一次的速度更新标签_Ios_Cocoa Touch_Nstimer_Cllocation - Fatal编程技术网

Ios 以每x秒一次的速度更新标签

Ios 以每x秒一次的速度更新标签,ios,cocoa-touch,nstimer,cllocation,Ios,Cocoa Touch,Nstimer,Cllocation,我正在开发我的第一个iPhone应用程序。我必须每x秒用设备速度更新一个标签。我已经创建了自己的CLController,可以获得设备速度,但我不知道是否需要使用NSTimer来更新标签。我该怎么做呢?你说得对,你必须使用NSTimer。 您将在x秒后调用一个方法并更新标签 [NSTimer scheduledTimerWithTimeInterval:x target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]

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

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

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

-(void)updateLabel
{
    // update your label
}

你可以这样安排计时器

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:YOUR_INTERVAL 
                       target:self 
                       selector:@selector(updateLabel) 
                       userInfo:nil 
                       repeats:YES];
现在,下面的方法将在每个_间隔(以秒为单位)期间被调用

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

[timer invalidate];

别忘了将计时器添加到runloop,否则它不会做任何事情。谢谢!我可以在NSTimer选择器中使用带参数的方法吗?您还需要通过将计时器添加到MainRunLoop
[[nsrunlop MainRunLoop]addTimer:myTimer forMode:NSRunLoopCommonModes]来启动计时器
[timer invalidate];