Reputation: 4999
How can I make a timer that counts down from 3 and then runs a method? How would I do that?
Upvotes: 1
Views: 786
Reputation: 22767
- (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];
Upvotes: 1
Reputation: 170839
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
Upvotes: 2
Reputation: 1266
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];
Upvotes: 3