How can I make a timer that counts down from 3 and then runs a metho开发者_高级运维d? How would I do that?
Is that different from a timer counting from 0 to 3? It will still wait three seconds, either way.
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(myMethod:) userInfo:nil repeats:NO];
Better way might be to use performSelector:withObject:afterDelay:
method:
[self performSelector:@selector(myMethod) withObject:nil afterDelay:3.0f];
Or in case method takes 1 parameter:
[self performSelector:@selector(myMethod:) withObject:parameter afterDelay:3.0f];
If method takes multiple parameters you'll need to use NSInvocation
class
- (void) handleTimer: (NSTimer *) timer
{
do some work here...
} // handleTimer
// at some point in your controller
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 3.0
target: self
selector: @selector(handleTimer:)
userInfo: nil
repeats: NO];
精彩评论