April 10, 2009 § Leave a comment
I am showing 2 ways of creating and running NSTimer. First approach start the timer immediately and another approach start the timer only it is put into the run loop.
This timer start immediately
// Create a timer
// timer will fire myMethod: to myObject in 5 sec. It adds the timer to the run loop.
timer = [NSTimer scheduledTimeWithTimeInterval:5.0 target:myObject selector:@selector(myMethod:) userInfo:nil repeats:NO];
This timer start later waiting for the run loop
// create a time to start later
timer = [NSTimer timerWithTimeInterval:5.0 target:myObject selector:@selector(myMethod:) userInfo:nil repeats:NO];
// timer will start only this line is added and put into the run loop
[[NSRunLoop currentRunLoop] addTimer:timer forMode: NSDefaultRunLoopMode];
// myMethod: argument is NSTimer
– (void) myMethod:(NSTimer*) timer;