これが私の状況です。正常に動作するが、電話をかけても停止しないように見えるカウントダウンアプリを作成していますが[stopWatchTimer invalidate];
、その理由がわかりません。これが私のコードです:
- (IBAction)btnStartPressed:(id)sender {
//Start countdown with the time on the Date Picker.
timeLeft = [pkrTime countDownDuration];
[self currentCount];
lblTimer.text = time; //sets the label to the time set above
pkrTime.hidden = YES;
btnStart.hidden = YES;
btnStop.hidden = NO;
//Fire this timer every second.
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/1.0
target:self
selector:@selector(reduceTimeLeft:)
userInfo:nil
repeats:YES];
}
- (void)reduceTimeLeft:(NSTimer *)timer {
//Countown timeleft by a second each time this function is called
timeLeft--;
// Get the system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
// Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:timeLeft sinceDate:date1];
// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
int sec = [conversionInfo second];
int min = [conversionInfo minute];
int hour = [conversionInfo hour];
NSString *seconds = [NSString stringWithFormat:@"%d",sec];
NSString *minutes = [NSString stringWithFormat:@"%d",min];
if (sec <= 9)
seconds = [NSString stringWithFormat:@"0%d", sec];
if (min <= 9)
minutes = [NSString stringWithFormat:@"0%d", min];
if ([conversionInfo hour] == 0)
time = [NSString stringWithFormat:@"%@:%@", minutes, seconds];
else
time = [NSString stringWithFormat:@"%d:%@:%@", hour, minutes, seconds];
lblTimer.text = time; //sets the label to the time set above
NSLog(@"%d", timeLeft);
if (timeLeft == 0) {
[self timerDone];
[stopWatchTimer invalidate];
stopWatchTimer = nil;
}
}
-(void)timerDone {
pkrTime.hidden = NO;
btnStart.hidden = NO;
btnStop.hidden = YES;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Timer Done" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
[alert show];
[self playAlert];
}
問題を教えてください...コードの問題がどこにも見つかりません!