我的代码如下所示。我试图在窗口的屏幕上显示一个计数器,并在计数限制结束后启用一个按钮。
当我在 nstimer 之前和之后运行代码时,nstimer 仅打印一次,并且时间倒计时功能没有打印任何内容。请有人帮助我。
- (void)startTimer:(NSInteger)count;
{
NSLog(@"Entered the start timer function");
self.countLimit = count;
[self.lbCount setStringValue:[NSString stringWithFormat:@"%ld",self.countLimit]];
NSLog(@"Before nstimer");
@try{
self.timeCounter = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeCountDown) userInfo:nil repeats:YES];
}
@catch (NSException* e) {
NSLog(@"%@",[e description]);
}
NSLog(@"After nstimer");
}
- (void)timeCountDown
{
NSlog(@"Entered the function");
self.countLimit = self.countLimit - 1;
NSLog(@"Just entered the function time countdown");
if (self.countLimit < 0) {
NSLog(@"Entered count limit less than 0");
[self.timeCounter invalidate];
self.timeCounter = nil;
[self.btContinue setEnabled:YES];
return;
}
[self.lbCount setStringValue:[NSString stringWithFormat:@"%ld",self.countLimit]];
}