2013-07-06 4 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

Укажите свой код, пожалуйста. –

+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

спасибо за ответ, что решила проблему. но знаете ли вы, почему приложение падает, когда я нажимаю ok и увольняю предупреждение? – EvilAegis