Friday, June 18, 2010

A Intro to Timers

One of the easiest things to set up in a iPad/iPhone app is a timer, and I'll show an example in this post.

The timer exists in every app, so rather than alloc and init, we schedule an event on the system timer:

[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(timerDone:) userInfo:nil repeats:NO];


Most of this is simple. The Interval is in seconds. The target is where the selector is located, and the selector is the method that is called when time is up. userInfo can be used to pass things to the timer, which are then passed through to the selector in case it will need something to correctly handle the event. Repeats is set to NO because I don't want to repeat this event.


The handler is below:

/**

Called by the timer when it exits.

*/

-(void) timerDone:(NSTimer *) timer{

[self.view removeFromSuperview];

}


This is a simple method that doesn't require the userInfo field, but if it did, I would have added it to the parameter list and used it in this method.


--- RTG

No comments:

Post a Comment