-1

我的代码如下所示。我试图在窗口的屏幕上显示一个计数器,并在计数限制结束后启用一个按钮。

当我在 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]];


}
4

2 回答 2

3

正如API 文档中详述的那样,您的计划方法必须带有一个NSTimer *参数:

- (void)timeCountDown:(NSTimer *)timer

不要忘记在scheduledTimerWithTimeInterval调用中更改您的选择器以反映这一点:

// There's a colon after selector name to denote
// the method takes one parameter
self.timeCounter = [NSTimer scheduledTimerWithTimeInterval:1 
    target:self selector:@selector(timeCountDown:) userInfo:nil repeats:YES];
于 2013-10-25T12:29:48.893 回答
0

如果您不想作为争论 NSTimer* 传递,请在您的计时器之后使用下面

[self.timeCounter fire];
于 2013-10-25T13:13:50.540 回答