NSTimer未触发预定方法

时间:2013-10-25 12:23:03

标签: macos cocoa nstimer

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

当我在nstimer之前运行代码并且在nstimer只打印一次后没有从时间倒计时函数打印任何内容时。有人pelase帮助我。

- (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]];


}

2 个答案:

答案 0 :(得分:3)

详见API docs,您的预定方法必须采用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];

答案 1 :(得分:0)

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

[self.timeCounter fire];