Ravi
Ravi

Reputation: 8309

UIAlertView + Modal View Controller doesn't work

I have a UIAlertView that has the buttons "OK" and "Cancel". I'd like to present a modal view controller when the OK button is pressed. Here's what I have done so far:

  1. Created the UIAlertView box. Implemented UIAlertViewDelegate protocol. Implemented (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex method.
  2. In the above method, when buttonIndex == 0, I'm trying to do something to the effect of:

    if (buttonIndex == 0)
    {
        ModalViewController *mdvc = [[[ModalViewController alloc] initWithNibName:nil bundle:nil] autorelease];
        [self presentModalViewController:mdvc animated:YES];
    }
    

As it turns out, the modal view does not present itself. I tried many other approaches but they are just making it complex and making me create a lot of unnecessary variables. There MUST be an easier way.

Some Extra Information:

  1. If it matters in anyway, this is an OpenGL ES application.
  2. If I invoke [self presentModalController:] as a result of a UIButton press, it does work as expected - I see the modal view controller.

Upvotes: 0

Views: 2178

Answers (4)

Craig
Craig

Reputation: 51

I was having this problem as well. It seems clear the alert must be gone before the modal can present, so you're looking for a way to know the alert is gone. Skimming the docs, there is a simple way.

Instead of presenting your modal when this is called:

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex

Use:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

This worked like a charm for me. From the documentation:

This method is invoked after the animation ends and the view is hidden.

Upvotes: 5

Ravi
Ravi

Reputation: 8309

Alright... so I fixed this myself. As I said, this was in an OpenGL ES application, so I set some global bools and called the [self presentModalViewController] in the drawFrame method. This is definitely not the best method, I know, but in the time-crunch I am in, there seems to be no better solution!

The issue is certainly delay-related but performSelector:withObject:afterDelay doesn't seem to be sufficient!

Upvotes: 1

gamozzii
gamozzii

Reputation: 3921

The Cancel button has index of 0, the OK button will have index of 1. Are you sure you are doing the action on the correct button index? (i.e. if you press 'Cancel' does it show the modal?).

Upvotes: 0

Mike Hay
Mike Hay

Reputation: 2856

First, move that code to a new method named presentModal:

- (void)presentModal:(UIViewController *)mvc {
    [self presentModalViewController:mvc animated:YES];
}


Then, in the method where you handle the response from the UIAlertView, call this new method, like this

ModalViewController *mdvc = [[[ModalViewController alloc] 
                                           initWithNibName:nil 
                                                    bundle:nil] autorelease];
[self performSelector:@selector(presentModal:) 
           withObject:mdvc 
           afterDelay:0.0];


That will postpone the execution of the presentModal: method until after the method handling the UIAlertView has returned, and after the UIAlertView has been removed from the screen.

Upvotes: 0

Related Questions