2013-07-06 55 views
0

我想向主線程發送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]; 
} 
+0

提供您的代碼,請。 – 2013-07-06 06:00:28

+0

完成。用代碼更新了問題。 – EvilAegis

回答

0

聲明你alertView作爲伊娃或「@property」,如果它已經存在,不顯示下一個警報。

或者關閉第一個提示並調出下一個提示。

例如,當你想提出警告:

if(self.alertView == NULL) 
{ 
    self.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"); 
} 

當它駁回,使用委託方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    // setting this to NULL means that another alert can be displayed 
    self.alertView = NULL; 
} 
+0

感謝您的回答,解決了這個問題。但是你知道爲什麼應用程序崩潰時,我按下確定並關閉警報? – EvilAegis

相關問題