Reputation: 8309
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:
(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
method.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:
Upvotes: 0
Views: 2178
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
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
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
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