2017-03-27 46 views
2

我使用NSTimer一旦的NSTimer停止解僱我的目標C視圖控制器

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES]; 

然後我60秒倒計時程序

-(void) updateCountdown { 
    int hours, minutes, seconds; 

    secondsLeft++; 
    hours = secondsLeft/3600; 
    minutes = (secondsLeft % 3600)/60; 
    seconds = (secondsLeft %3600) % 60; 


    time.text = [NSString stringWithFormat:@"%02d", seconds]; 

} 

我的問題是後60秒解僱我倒計時查看控制器頁面..任何人都可以幫助我

+0

功能替換'重複:YES] ;'帶'重複:NO];'在'NSTimer'的初始化中。 – iphonic

+0

你想在60秒後解散你的控制器或不清楚問題 –

+0

是的,我想在60秒後解僱我的視圖控制器 –

回答

7

如果repeats: YES];檢查像你的seconds達到或大於60,無效的定時器或解散你的VC

if (seconds >60) 
{ 
[timer invalidate]; 
[self dismissViewControllerAnimated:YES completion:nil]; 
} 

別人打電話給你的計時器使用一次repeats: NO];

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: NO]; 

,並呼籲像

-(void) updateCountdown { 
    [timer invalidate]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 

} 
+0

好的答案brother.I up voteed。 – user3182143

+0

我無法在我的方法中訪問定時器,因此如何使該方法無效??安博.Karthick – Akshay

2
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown:) userInfo:nil repeats: NO]; // set No 

-(void) updateCountdown:(NSTimer *)timer 
{ 
    [timer invalidate]; 
    // dismiss your controller 
} 
相關問題