viewDidLoad
에서 내 타이머를 호출하고 있습니다. 타이머가 selector를 처음 호출 할 때 예상되는 결과가 좋습니다. 그러나 점차 호출이 이루어지면 함수는 선택기에 의해 여러 번 호출됩니다. 나는 출력 수가 증가하고 있음을 보여주는 NSLog
을 기록했다. 아래 코드가 있습니다. 그것이 상황을 분명하게 바란다.NSTimer에서 셀렉터 기능을 여러 번 호출하지 못하게하려면 어떻게해야합니까?
-(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];
}
로그 출력을 게시 할 수 있습니까? UIButton *하지만 [[UIButton alloc] init]; '1) 버튼이 뷰에 추가되지 않고 2) 확실하게 메모리가 누출 되었습니까? – trojanfoe
ARC를 사용하는 경우 새어 나지 않습니다. –