I want to start an NSTimer at viewdidload and then execute a few void functions at specific intervals. How do I do this?
Thanks!
I want to start an NSTimer at viewdidload and then execute a few void functions at specific intervals. How do I do this?
Thanks!
Here's your basic pattern:
MyViewController.h:
...
@interface MyViewController : UIViewController
{
...
NSTimer* timer;
...
}
...
MyViewController.cpp:
...
static const NSTimeInterval TIMER_INTERVAL = 1.0;
...
- (void)dealloc
{
[self stopTimer];
...
[super dealloc];
}
...
- (void)viewDidLoad
{
...
[self startTimer];
...
}
...
- (void)startTimer
{
[self stopTimer];
timer = [NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL
target:self
selector:@selector(timerCallback)
userInfo:nil
repeats:YES];
[timer retain];
}
...
- (void)stopTimer
{
if (timer)
{
[timer invalidate];
[timer release];
timer = nil;
}
}
...
- (void)timerCallback
{
someFunction();
[self someMethod];
}