开发者

increment NSTimer

I have this method in my app :

- (void)initializeTimer{
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:theI开发者_如何学运维nterval target:self 
                               selector:@selector(move:) userInfo:nil repeats:YES];
}

I notice that if i change the interval the timer still with the old value.


You can't change the interval once the timer is created. Look at the NSTimer docs on Apple's site. You'd have to invalidate and release the timer and then create a new one with the new interval.


I notice that if i change the interval the timer still with the old value.

That's because calling functions in C and sending messages in Objective-C is pass-by-value, not pass-by-name. You do not pass your theInterval variable; you pass the value that is in that variable at that time. The method receives only the value; it has no knowledge of where you might have passed it from. Putting a different value in the theInterval variable, or in any other storage, has no effect on the timer, because the timer does not know of or care about that variable or anything else outside itself.

The only way you could be able to change the timer's interval would be to send it a setTimeInterval: message, and as Marc W said in his answer and as the docs will back up, NSTimer objects do not respond to such a message. Thus, there is no way to change the interval of an existing timer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜