Tutorial :Bug in AlertView on iPhone?


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?


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.


The delegate is so that you can be notified when the alert is dismissed, and which button was used to dismiss it. It doesn't impact whether the alert is dismissed at all.

The alert will remain visible until it is dismissed either by you tapping a button (if any - they are not required) or you call either [UIAlertView dismissWithClickedButtonIndex:animated] or the (undocumented) dismiss method of the alert instance.

It looks like (as Cobbal suggested), alert 2 is popping up over alert 1, you dismiss alert 2, and alert 1 remains there (until it itself is dismissed).

Is there a particular reason you want to show a new alert while another is still showing? Perhaps some more context would help us to get to the root of the issue, which I suspect may be a design issue.

[edit] coming back to this and reading again, I wonder if what you are asking about with the delegate method is whether you should be showing alert 2 from there? In which case that's probably what you want - whether directly or indirectly. By indirectly I mean that you may have some state set elsewhere that determines whether alert 2 should be shown (or the circumstances that lead to it). That state (a flag, perhaps) could be set when you show the first alert, and cleared when the alert is dismissed (from the delegate method).


The reason this happens is because UIAlertView doesn't block while it's showing. Any code written after showing an alertview will run straight after the alert is shown.

What you should have is two different methods. One that does something and then shows an alert, and then another that does something and shows another alert.

Kick off the first method to do something and show an alert, and then hook into the alert's delegate method, and when you get the callback from the alertview, run the other method.

This way, the second part of the process won't happen until the user has pressed OK on the alert in first part of the process.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »