2 つのアラート ビューをメイン スレッドに送信しようとしていますが、最初のアラート ビューが閉じられておらず、2 番目のアラート ビューが表示されると、アプリケーションがクラッシュします。どうすればこれを回避できますか?
- (void)viewDidLoad
{
[super viewDidLoad];
gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
self.date1 = [NSDate dateWithTimeInterval:[testTask timeInterval] sinceDate:[NSDate date]];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
[timer fire];
}
-(void)timerAction:(NSTimer *)t{
NSDate *now = [NSDate date];
NSDateComponents *components = [gregorianCalendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:now toDate:self.date1 options:0];
NSString *timeRemaining = nil;
if([now compare:self.date1] == NSOrderedAscending){
timeRemaining = [NSString stringWithFormat:@"%02d:%02d:%02d", [components hour], [components minute], [components second]];
NSLog(@"works %@", timeRemaining);
} else {
timeRemaining = [NSString stringWithFormat:@"00:00:00"];
[self.timer invalidate];
self.timer = nil;
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:[testTask taskName] message:@"Time is up!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
NSLog(@"ended");
}
timerLabel.text = timeRemaining;
[timerLabel setNeedsDisplay];
[self.view setNeedsDisplay];
}