2010-07-05 3 views
0

나는 HH와 스톱워치 만들려고 한 다음 SS, 코드가로 : MM 예상대로 출력의 형식은 XX입니다 출력이 일초 씩 증가 않습니다는 NSTimer - 스톱워치

-(IBAction)startTimerButton; 
{ 
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showActivity) userInfo:nil repeats:YES]; 
} 


-(IBAction)stopTimerButton; 
{ 
    [myTimer invalidate]; 
    myTimer = nil; 
} 


-(void)showActivity; 
{ 
    int currentTime = [time.text intValue]; 
    int newTime = currentTime + 1; 
    time.text = [NSString stringWithFormat:@"%.2i:%.2i:%.2i", newTime]; 
} 

있지만 : YY : ZZZZZZZZ, 여기서 XX는 초입니다.

누구나 어떤 생각이라도 ??

답변

6

귀하의 stringWithFormat 3 개 정수를 요구하지만 당신은 하나를 전달하고;) 여기

은 내가 당신이하려는 생각해야하기 전에 사용했던 일부 코드 :

- (void)populateLabel:(UILabel *)label withTimeInterval:(NSTimeInterval)timeInterval { 
    uint seconds = fabs(timeInterval); 
    uint minutes = seconds/60; 
    uint hours = minutes/60; 

    seconds -= minutes * 60; 
    minutes -= hours * 60; 

    [label setText:[NSString stringWithFormat:@"%@%02uh:%02um:%02us", (timeInterval<[email protected]"-":@""), hours, minutes, seconds]]; 
} 

는 타이머와 함께 사용하려면 다음을 수행하십시오

... 
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES]; 
    ... 

- (void)updateTimer:(NSTimer *)timer { 
    currentTime += 1; 
    [self populateLabel:myLabel withTimeInterval:time; 
} 

을 currentTime을 사용하면 두 번째마다에 의해 계산하려는 NSTimeInterval입니다.

+0

감사합니다. YY, ZZ가 위 코드로 표시되는 형식이었습니다. 하지만 그것을 변경했습니다 : time.text = [NSString stringWithFormat : @ "% 02i : % 02i : % 02i", 초/(60 * 60), 초/60, 초]; 당신이 제안한 것을 기반으로하고 형식은 HH : MM : SS가 아니지만 1 초 후에 카운트를 멈 춥니 다. 어떤 아이디어입니까? – Stephen

+0

메소드의 첫 번째 줄은 [time.text intValue]입니다. HH : MM : SS와 같은 문자열의 intValue가 없기 때문에 intValue는 매번 0을 반환합니다. currentTime을 저장하려면 클래스에 변수를 추가해야합니다. 이전에 사용한 적이있는 코드에 대한 편집을 참조하십시오. – deanWombourne

+0

모두 고마워요. – Stephen