[iPhone] Cannot display two Modal View Controller after each other

Discussion in 'iPhone/iPad Programming' started by maxm007, Jul 8, 2008.

  1. macrumors newbie


    This is for BETA 7.

    Here is a simplified version of my problem.

    In my application root view controller , the user presses a 'Start Wizard' button and they get modal view controller A presented which contains a 'Step 1 Complete' button. When that 'Step 1 Complete' button is pressed , ModalVC A dismisses itself and delegates an "I'm done" event to the root view controller , which presents another ModalVC B to the user with a 'Step 2 complete'. So basically imagine a Wizard of Modal View Controllers. Try to make one, it doesn't work.

    The problem is that MODALVC B doesn't get displayed. It almost seems that when ModalVC A dismisses itself from the parent , this does not go in effect until the main thread goes back into its message loop for a redraw or something and it ignores the subsequent 'PresentModalViewController : nextcontroller' call.

    The only way to make it work is to have 2 buttons ('Do Step 1' and 'Do Step 2') is the root view controller and have the user press them individually. Of course that's not what I want.
  2. macrumors member


    There are problems with displaying more than one VC in a row. To work-around the problem, allow your program to spin in the message loop after dismissing first VC. Set up a timer event to pull yourself out of the message loop after 1ms and proceed to next VC.

    I accept donations in form of beer. :D
  3. macrumors member

    I think I know better solution than timers. All you need to do is to present next modal view on top of the first modal view (by calling -presentModalView... on the first modal view controller). Then simply dismiss first modal view and second one view will disappear together with the first one :)
  4. macrumors 68030


    You can also use performSelector:withObject:afterDelay to do things like this. Set the delay to 0 or 0.1 or something like that. The perform will then happen very quickly. No timers required.
  5. macrumors newbie

    ok but how do actually dismiss that first modal view from within the 2nd one? the dismiss modal view method only takes in the current modal view, no?
  6. Moderator


    Staff Member

    So, are you looking to dismiss the first modal while the second one stays active? So that when the second one is finally dismissed you return to the view that was shown when the first modal was presented?
  7. macrumors newbie

    Ok so my setup is that I have up to 9 modal views displaying one on top of the other. With the last modal view I would like it to dismiss all the preceding modal views when it is dismissed. Is that possible? Thanks
  8. Moderator


    Staff Member

    9? Wow, that sounds like a lot. I hope the app justifies that many.

    Yes, it's possible. You just somehow need to find a way for the ninth modal view to know about the first modal view. I'd suggest setting it as an ivar in the appDelegate. Then you can call:
    [appDelegate.firstModalViewController dismissModalViewControllerAnimated:YES];
    This should dismiss all the other eight modal views on the stack above it, as well.
  9. macrumors newbie

    Thanks for replying. The thing is that I don't have 9 unique modal views. Its just the same view controller being presented in modal form each time. Maybe I should have a unique modal view for each one...

    Here's how I call it from within each modal window. (AddViewController is what gets presented in the modal view):

    if(avController == nil)
    		avController = [[AddViewController alloc] initWithNibName:@"AddView" bundle:nil];
    	if(addNavigationController == nil)
    		addNavigationController = [[UINavigationController alloc] initWithRootViewController:avController]; 
    	[self.navigationController presentModalViewController:addNavigationController animated:YES];
  10. Moderator


    Staff Member

    It won't matter whether the views are unique or not. What matters is that they all are sitting on the modal view stack.

    If they're all the same view, why wouldn't you want to reuse them?

    This looks funky. If you are using a UINavigationController, why do you need to present modal views when you could just be pushing the new views onto the navigation stack, via pushViewController:animated:?
  11. macrumors newbie

    Perfect! Using the navigation controller to do it all was a much more intelligent way of doing things. Thanks!

Share This Page