UIAlertView is not waiting

I use several UIAlertViews in my code as follows

-(void) myfunc { myAlertView1 = [[UIAlertView alloc] initWithTitle:@"Message" message:[list objectAtIndex:1] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [myAlertView1 show]; [myAlertView1 release], myAlertView1 = nil; { do something } myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[list objectAtIndex:1] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [myAlertView show]; [myAlertView release], myAlertView = nil; } 

When I run the program in the simulator, I see myAlertView1 (Message) briefly and do not wait for the Ok button to click, then I see myAlertView (Error), which waits for the Ok button to click, and after that I see myAlertView1 (Message) again, and it waits for the OK button to click .

Logically, I want to see myAlertView1 (Message) and wait while the Ok button is pressed, and then click myAlertView (Error) and wait while the button is pressed. Did I miss something?

+4
source share
3 answers

UIAlertView not modal, as you might expect. You must wait for your delegate to receive alertView:didDismissWithButtonIndex: before creating and showing the second UIAlertView

+12
source

Here's how you can make your dialogue modal.

I came across this while researching a similar question from a MonoTouch / C # user, so I wrote this sample for him. The same pattern can be trivially ported to Objective-C.

To do this, you can simply start mainloop manually. I was not able to stop mainloop directly, so instead I started mainloop for 0.5 seconds and wait for the user to respond.

The following function shows how you could implement a modal query using the above approach:

 int WaitForClick () { int clicked = -1; var x = new UIAlertView ("Title", "Message", null, "Cancel", "OK", "Perhaps"); x.Show (); bool done = false; x.Clicked += (sender, buttonArgs) => { Console.WriteLine ("User clicked on {0}", buttonArgs.ButtonIndex); clicked = buttonArgs.ButtonIndex; }; while (clicked == -1){ NSRunLoop.Current.RunUntil (NSDate.FromTimeIntervalSinceNow (0.5)); Console.WriteLine ("Waiting for another 0.5 seconds"); } Console.WriteLine ("The user clicked {0}", clicked); return clicked; } 
0
source

Here you can do it as follows:

 -(void) myfunc { myAlertView1 = [[UIAlertView alloc] initWithTitle:@"Message" message:[listobjectAtIndex:1] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [myAlertView1 show]; [myAlertView1 release], myAlertView1 = nil; { do something } } 

And when you get a warning, you can click the OK button, which calls another method to open another warning.

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if(buttonIndex==0) { [self open_secondAlert]; } } -(void)open_secondAlert { myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[list objectAtIndex:1] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [myAlertView show]; [myAlertView release], myAlertView = nil; } 

Please let me know if you have any questions.

Thanks, Best Regards, Gurprit

0
source

All Articles