Objective-C NSTimer

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
NSTimer *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;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

What’s this?

You are currently reading Objective-C NSTimer at Web Builders.


%d bloggers like this: