2011-05-13 5 views
0

다음 코드를 컴파일했지만 명백한 런타임 오류가 없습니다. 그러나, 나는 그것을 실행할 때 00 : 00 : 01에 동결된다. 초 속성 만 표시하면 작동합니다. 누구든지이 코드에서 놓친 명백한 감독을 보았습니까? 시작 버튼으로 잠재적 인 메모리 누수가 있다는 것을 알고 있지만, 결국에는 해결할 것입니다.스톱워치 디스플레이 문제

미리 감사드립니다.

#import "StopwatchViewController.h" 

@implementation StopwatchViewController 

- (IBAction)start{ 

    //creates and fires timer every second 
    myTimer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showTime) userInfo:nil repeats:YES]retain]; 
} 
- (IBAction)stop{ 
    [myTimer invalidate]; 
    myTimer = nil; 
} 

- (IBAction)reset{ 

    [myTimer invalidate]; 
    time.text = @"00:00:00"; 
} 

(void)showTime{ 

    int currentTime = [time.text intValue]; 

    int new = currentTime +1; 

    int secs = new; 
    int mins = (secs/60) % 60; 
    int hours = (mins/60); 

    time.text = [NSString stringWithFormat:@"%.2d:%.2d:%.2d",hours, mins, secs]; 
} 

답변

3

당신은

int currentTime = [time.text intValue]; 

에서 0을 얻고 있기 때문에 text에서의 문자열 :

@"00:00:00" 

int로 변환 할 수 없습니다, 그래서 때마다 타이머 화재, 1에서 0을 더하고 1을 얻으면 표시됩니다. 수학은 부정확 할 것입니다. 왜냐하면 분과 초가 "base-60"*이기 때문입니다. 총 초를 다시 얻으려면 시간/분/초를 분리하기 위해 수행하는 수학의 역순을 수행해야합니다. currentTime을 ivar로 만들고 총 시간 (초)을 유지하면됩니다.


* 그게 실제로 부르는 것이 아닙니다. 특정 단어가있을 것입니다.

+0

초만 추적하고 텍스트 필드의 값을 모델 데이터로 사용하는 대신 형식을 지정하는 제안에 동의합니다. – Wevah

+0

제안에 큰 감사드립니다. 나는 왜 내가 그러한 멍청이 행동을했는지 모른다. 나는 그것을 수정하고 수정 된 버전을 게시 할 것입니다. 도움을 감사하십시오. – dman

+0

@deesho : 천만에요. 행운을 빕니다! –

2
- (IBAction)start{ 

    currentTime = 0; 

    //creates and fires timer every second 
    myTimer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showTime) userInfo:nil repeats:YES]retain]; 
} 

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

- (IBAction)reset{ 

    [myTimer invalidate]; 
    time.text = @"00:00:00"; 
} 

- (void)showTime{ 

    currentTime++; 

    int secs = currentTime % 60; 
    int mins = (currentTime/60) % 60; 
    int hour = (currentTime/3600); 


    time.text = [NSString stringWithFormat:@"%.2d:%.2d:%.2d",hour, mins, secs]; 

}