-5

I want to make an action when every second is passed, for example I got a label and I want it every second to do this :

theLabel.text = @"Hey";

So, how I'll do it? I think I'll need to use the NSTimer right?

Thanks in Advance

rmaddy
  • 314,917
  • 42
  • 532
  • 579
AFB
  • 550
  • 2
  • 8
  • 25

3 Answers3

4

Just use a NSTimer:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doSomething:) userInfo:nil repeats: YES];

And to stop it:

[timer invalidate];

Find the documentation here:

NSTimer Doc

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
2

A "plan B" solution:

[self performSelector:@selector(foo) withObject:nil afterDelay:{some time interval}];

-(void)foo
{
    //do something..
    [self performSelector:@selector(foo) withObject:nil afterDelay:{some time interval}];
}
Mert Buran
  • 2,989
  • 2
  • 22
  • 34
1

Yes @kevin you are right NSTimer is the best

just initialize your timer on did load

NSTimer *timerCounter = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doMyWork) userInfo:nil repeats: YES]; //make repeats YES

and do the below

-(void)doMyWork
{
   theLabel.text = @"Hey";
   // Or what ever you want to do
}
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86