2012-06-27 171 views
1

我創建倒數計時器,顯示在標籤上,現在我想添加一個函數,eberytime用戶按下一個按鈕,它將添加30秒到倒數計時器。 我試圖自己做,但我沒有成功(我得到了一個錯誤,一次它停止我的計時器)我該怎麼做? 這裏是我的代碼:推秒倒數計時器

-(IBAction)start:(id)sender 
{ 
    timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self  selector:@selector(updateTimer:) userInfo:nil repeats:YES]; 

} 

-(void)updateTimer:(NSTimer *)timer 
{ 
    currentTime -= 10 ; 
    [self populateLabelwithTime:currentTime]; 

    if(currentTime <=0) 
    { 
     [timer invalidate]; 
     //some code when countdown reach to 00:00:00 
    } 

} 


- (void)populateLabelwithTime:(int)milliseconds { 
    seconds = milliseconds/1000; 
    minutes = seconds/60; 


    seconds -= minutes * 60; 

    NSString * countdownText = [NSString stringWithFormat:@"%@%02d:%02d:%02d", (milliseconds<[email protected]"-":@""), minutes, seconds,milliseconds%1000]; 
    countdownLabel.text = countdownText; 

} 

-(IBAction)addTime:(id)sender 
{ 
    timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(addToCurrent:) userInfo:nil repeats:YES]; 
} 

-(void) addToCurrent:(NSTimer *)timerb; 
{ 
    seconds +=10; 
    [self populateLabelWithTime:seconds]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    currentTime = 2700; 

} 

的感謝!

+0

包括到底出了什麼問題以及得到的錯誤是有幫助的。 – cobbal

+0

這個應用程序是粉碎的... – cnaaniomer

+1

' - (IBAction)addTime:(id)sender {currentTime + = 30000; }' – Anne

回答

0

有沒有必要重新點擊每個按鈕的計時器。另外,當您嘗試將30秒添加回計時器時,它在我看來像是意外地修改了秒數而不是currentTime。

-(IBAction)addTime:(id)sender 
{ 
    currentTime += 30000; 
    [self populateLabelWithTime:currentTime]; 
}