开发者

Cocoa Touch - Timers

开发者 https://www.devze.com 2023-01-11 00:06 出处:网络
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 w

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消