我的计时器从viewDidLoad
. 计时器第一次调用选择器时,预期的结果很好。但是逐渐地,当进行调用时,选择器会以某种方式多次调用该函数。我记录了一个NSLog
显示输出数量正在增加的结果。我有下面的代码。希望它使情况清楚。
-(void)viewDidLoad
{
remainingTicks = 10;
[self updateLabel];
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(handleTimerTick)
userInfo:nil
repeats:YES];
}
-(void)handleTimerTick
{
remainingTicks--;
[self updateLabel];
if (remainingTicks <= 0) {
[myTimer invalidate];
myTimer = nil;
UIButton *but = [[UIButton alloc] init];
if (answerAt == 0) {
[buttonA setBackgroundColor:[UIColor greenColor]];
}
else if (answerAt == 1) {
[buttonB setBackgroundColor:[UIColor greenColor]];
}
else if (answerAt == 2) {
[buttonC setBackgroundColor:[UIColor greenColor]];
}
else {
[buttonD setBackgroundColor:[UIColor greenColor]];
}
[self performSelector:@selector(next:) withObject:but afterDelay:1.5 ];
}
}
-(void)updateLabel
{
timerLabel.text = [[NSNumber numberWithUnsignedInt: remainingTicks] stringValue];
}