2012-02-28 6 views
0

안녕하세요. 앱에 스톱워치가 있습니다.Xcode의 스톱워치 문제

나는 스톱워치에서 시작, 중지 및 재설정 버튼을 가지고 있습니다.

정지 및 리셋 버튼이 시작 일종의 작동

작동합니다.

사용자가 처음 시작 버튼을 클릭하면 스톱워치가 시작됩니다. 그들이 정지 버튼을 클릭하고 다시 시작 버튼을 클릭하면, 그것은 다시 스톱워치를 시작합니다.

내가 누락 된 코드는 무엇입니까?

.H

IBOutlet UILabel *stopWatchLabel; 
    NSTimer *stopWatchTimer; // Store the timer that fires after a certain time 
    NSDate *startDate; // Stores the date of the click on the start button 
    @property (nonatomic, retain) IBOutlet UILabel *stopWatchLabel; 
    - (IBAction)onStartPressed; 
    - (IBAction)onStopPressed; 
    - (IBAction)onResetPressed; 
    - (void)updateTimer 

하는 .m

- (void)updateTimer{ 

    NSDate *currentDate = [NSDate date]; 
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; 
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"HH:mm:ss:SSS"]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; 
    NSString *timeString=[dateFormatter stringFromDate:timerDate]; 
    stopWatchLabel.text = timeString; 

    } 

    - (IBAction)onStartPressed { 
    startDate = [NSDate date]; 

    // Create the stop watch timer that fires every 10 ms 
    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 
                target:self 
               selector:@selector(updateTimer) 
               userInfo:nil 
               repeats:YES]; 
    } 

    - (IBAction)onStopPressed { 
    [stopWatchTimer invalidate]; 
    stopWatchTimer = nil; 
    [self updateTimer]; 
    } 

    - (IBAction)onResetPressed { 
    stopWatchLabel.text = @"00:00:00:000"; 
    } 

어떤 도움도 좋은 것입니다.

건배

답변

1

위 코드에는 시작 - 중지 동작 중에 경과 한 시간이 저장되어 있습니다. 이렇게하려면 클래스 수준에서 NSTimeInterval totalTimeInterval 변수가 필요합니다. 처음에 또는 리셋 버튼을 누를 때 그 값은 0으로 설정됩니다. updateTimer 방법에서 다음 코드를 바꿔야합니다.

NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; 
totalTimeInterval += timeInterval; 
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:totalTimeInterval ]; 

감사

0

TimeInterval이 우리 updateTimer 기능 totalTimeInterval 1 = 1이 될 것이다 전화 등 1,2,3,4,5 같은

totalTimeInterval = totalTimeInterval + timeInterval -> 0 = 0 + 1 

다음 시간을 증가 + 2이므로 totalTimeInterval은 3이됩니다.

그래서 totalTimeInterval을 표시하면 1, 3, 6, ... 등이됩니다.

  1. 먼저 당신이

  2. updateTimer 방법을 사용하여 다음 코드를 교체해야합니다 클래스 레벨에 NSTimeInterval totalTimeInterval 및 NSTimeInterval TimeInterval이 변수가 필요합니다.

    timeInterval = [currentDate timeIntervalSinceDate:startDate]; 
    timeInterval += totalTimeInterval; 
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 
    
  3. 하고 다음 코드 onStopPressed 방법.

    totalTimeInterval = timeInterval; 
    

감사합니다.