Update a label with speed every x seconds

后端 未结 2 1500
广开言路
广开言路 2021-01-21 05:32

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

2条回答
  •  庸人自扰
    2021-01-21 06:15

    You can schedule the timer like this

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

    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];
    

提交回复
热议问题