-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 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];
于 2013-10-25T12:29:48.893 に答える
0

引数 NSTimer* として渡したくない場合は、タイマーの後に以下を使用します

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