Riccardo Queri
Riccardo Queri

Reputation: 1025

Update a label with speed every x seconds

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?

Upvotes: 3

Views: 2223

Answers (2)

Krishnabhadra
Krishnabhadra

Reputation: 34296

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

Upvotes: 7

Vaibhav Tekam
Vaibhav Tekam

Reputation: 2344

you are right you have to use NSTimer. You will be calling one method after x seconds and updating the label.

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

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

Upvotes: 2

Related Questions