Monday, April 8, 2013

Timer in xCode

1. Declare NSTimer in class ".h" file

@property (nonatomic, assign) NSTimer *timer;

2. Synthesize in class ".m" file

@synthesize timer;

3. Now use this, where you want. But always invalidate timer before use. So that timer could start from scratch

[timer invalidate];
timer = nil;
//Start The Timer

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
//if repeats is set to NO, then timer will run only once. otherwise it will be called, until you stop the timer or stop the application.

4. To Stop Timer: Just use the following code

[timer invalidate];
timer = nil;