2012-06-27 2 views
1

저는 라벨에 표시되는 카운트 다운 타이머를 만듭니다. 사용자가 버튼을 누르는 순간 카운트 다운 타이머에 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]; 
} 
관련 문제