I have two views that need to be shown modally, one after the other. This doesn’t work if we dismiss and show consecutively, like this:
[rootController dismissModalViewControllerAnimated: YES];
[rootController presentModalViewController: psvc animated: YES];
The second modal view simply doesn’t show up.
I’ve seen a fix that was something like this:
[rootController dismissModalViewControllerAnimated: YES];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[self performSelector: @selector(seekModal) withObject: nil afterDelay: 0.5];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
The problem is that this won’t work all the time (the delay needed is superior, sometimes).
Another possible fix would be to eliminate the animation:
[rootController dismissModalViewControllerAnimated: NO];
[rootController presentModalViewController: psvc animated: YES];
But I’d really like to keep the animation, to keep the feel that the first modal is out of the way. Any suggestions?
EDIT: The “correct” mechanism to do this in iOS5+ would be to use the
– dismissViewControllerAnimated:completion:method, and present the sequential view controller from the completion block.The viewcontroller that is being shown modally will have its viewDidDisappear:animated: method called once the modal-dismissal-animation is complete. AFIK this is the only place you can hook to initiate a subsequent presentModalViewController:animated: call.
I have a class that I use for presenting modal view controllers and it implements the logic you’re looking for via a callback to the presenting view controller once the dismissal is complete. To use this class, simply alloc/init an instance and present using the normal presentViewController:animated: call. Implement the following method on the presenting view controller:
This will be called at once the modal view controller is gone, and you can present a new modal view controller at this time.
One nice thing too – since this class is a specialization of UINavigationController, you can configure the navigationBar on/off as you like. The class also has built-in logic to show a dismiss button, as you like.
Here’s the class definition:
And the class implementation:
and, here’s how you can use it (in the context of some normal view controller):
and, here is the dismissal callback method, which presents a new modal view controller: