我正在使用此代码:
timer = [NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(update)
userInfo:nil
repeats:YES];
...
-(void)update {
NSDate *date = [NSDate date];
NSString *Currentdate = [date descriptionWithCalendarFormat:@"%b %d, %Y %I:%M:%S %p"
timeZone:nil
locale:nil];
lbl.text = Currentdate;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我想在运行时将计时器间隔从2秒更改为1秒.我怎样才能做到这一点?
提前致谢.
wil*_*lc2 10
创建后,您无法更改计时器间隔.
// You have to invalidate it...be careful,
// this releases it and will crash if done to an already invalidated timer
if (self.timer != nil) {
[self.timer invalidate];
self.timer = nil;
//... then declare a new one with the different interval.
timer = [NSTimer scheduledTimerWithTimeInterval:newInterval
target:self
selector:@selector(update)
userInfo:nil
repeats:YES];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2423 次 |
| 最近记录: |