开发者

iPhone NSTimer change repeat time

I have a static method in which I setup NSTimer, but sometimes I need to change repeat time. How to do that if I work with static method? Should be something like this:

+ (void)setupTimer:(BO开发者_运维问答OL)updateTime newTime:(int)time {
     NSTimer *timer;
     if (updateTime) {
         [timer invalidate];
         timer = nil;
     }
     timer = [NSTimer scheduledTimerWithTimeInterval:time 
                                              target:self 
                                            selector:@selector(myMethod:)
                                            userInfo:nil 
                                             repeats:YES];
}

But it's not working...and I know that I cannot invalidate in this case my timer...but also I cannot use NSTimer declared as member of class in static method...How to be ? thanks....


Just prefix the variable with static.

static NSTimer *timer;

If you overwrite the timer variable, you should invalidate it first, because otherwise you'll have a timer running, that you don't have access to anymore.


I think you should be able to write this without static functions but you can also do this using static functions. If there should be only one instance of that class then use a singleton-pattern.

for doing this with static functions then just use

static NSTimer *timer

but you also have to retain your new and also release the old timer because the function scheduledTimer... allocs an NSTimerinstance but also autoreleases it. If dont retain that timer then the timer will be deallocated later and will afaik not call your method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜