Ios 以每x秒一次的速度更新标签
我正在开发我的第一个iPhone应用程序。我必须每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]
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];