안녕하세요. 앱에 스톱워치가 있습니다.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";
}
어떤 도움도 좋은 것입니다.
건배