2013-10-25 41 views
-1

我的代碼如下。我試圖在窗口中的屏幕上顯示一個計數器,並在計數限制結束後啓用按鈕。NSTimer未啓動預定方法

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


} 

回答

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

如果你不想通過作爲NSTimer *的爭議,那麼在你的計時器之後使用下面的代碼

[self.timeCounter fire]; 
+1

這不會有預定的延遲,但對他來說沒用。 – occulus