简体   繁体   中英

UIAlertView does not wait

I am using multiple 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 simulator I see myAlertView1 (Message) briefly and it does not waits for Ok button click then I see myAlertView (Error) which waits for Ok button click and after that I again see myAlertView1 (Message) and it waits till OK button is clicked.

Logically I want to see myAlertView1 (Message) and wait till Ok button is clicked and then see myAlertView (Error ) and wait till button is clicked. Am I missing something here?

UIAlertView is not modal as one might expect. You should wait for your delegate to recieve alertView:didDismissWithButtonIndex: before creating and showing the second UIAlertView

Here is how you can make your dialog modal.

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

To do this, what you can do is to run the mainloop manually. I have not managed to stop the mainloop directly, so I instead run the mainloop for 0.5 seconds and wait until the user responds.

The following function shows how you could implement a modal query with 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;
}

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 alertview, you can click OK button which calls another method to open another alertview.

- (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 still have any questions.

Thanks, Best Regards, Gurprit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM