2012-06-27 178 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]; 
}