R – Bug in AlertView on iPhone

alertiphoneobjective c

I write a piece of code to "do something->show alert1->do something->show alert2".


    //do something
    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:@"Alert 1" 
                          message:nil 
                          delegate:nil 
                          cancelButtonTitle:@"OK" 
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    //do something  
    UIAlertView *alert2 = [[UIAlertView alloc] 
                           initWithTitle:@"Alert 2" 
                           message:nil 
                           delegate:nil 
                           cancelButtonTitle:@"OK" 
                           otherButtonTitles:nil];
    [alert2 show];
    [alert2 release];

And suddenly a strange thing happened to multiple AlertViews: It shows "Alert 1"->"Alert 2"(Press 'OK')->"Alert 1". Why "Alert 1" shows again? I haven't written any delegate method yet. Maybe a bug?(Thanks to cobbal, alert1 is still there when alert2 appears.)

I find -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex works well. Is the delegate method a common way to show multiple alertViews?

Best Answer

I would guess that alert 1 is shown and then covered by alert 2 since show isn't modal. When alert 2 is closed, alert 1 is still open.

To your second question, alertView:didDismissWithButtonIndex: may work better, but I haven't actually tested that.

Related Topic