dismissModalViewController Not Working

Discussion in 'iPhone/iPad Programming' started by stadidas, Dec 27, 2008.

  1. macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #1
    Hi everyone,

    I am having an issue with a modal view that I am using. I have a view controller setup and initialised with a xib file like so:

    Code:
    transactionViewController = [[TransactionViewController alloc] initWithNibName:@"TransactionView" bundle:nil];
    
    I then present this as a modal dialogue like this:

    Code:
    [[self navigationController] pushViewController:transactionViewController animated:YES];
    
    To dismiss this modal view, from TransactionViewController I do this:

    Code:
    [[self navigationController] dismissModalViewControllerAnimated:YES];
    
    However, in my instance of TransactionViewController, navigationController is nil, so the view is not dismissed. I have copied this idea from Apple's NavBar sample where it works perfectly well. The only difference is that my view controller that invokes the modal view is within a UITabBarController.

    I have no idea why the navigationController of my TransactionViewController instance is nil. If anyone could give me any pointers I would be most grateful.
     
  2. macrumors regular

    Joined:
    Mar 13, 2008
    #2
    (deleted duplicate)
     
  3. macrumors regular

    Joined:
    Mar 13, 2008
    #3
    try
    Code:
    [self dismissModalViewControllerAnimated:YES];
    the dismiss also works on the modal view controller itself.
     
  4. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #4
    Excellent! Thank you good sir :)
     
  5. macrumors 68040

    Joined:
    Apr 22, 2005
    #5
    You are not correctly presenting the modal view. You will want to use presentModalViewController rather than presentViewController. Then your dismissModalViewController function will work when called on the navigationController.
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    popViewControllerAnimated when you used pushViewController:animated:

    dismissModalViewControllerAnimated when you used presentModalViewController:animated:
     
  7. macrumors member

    Joined:
    Sep 20, 2010
    Location:
    Somewhere but not here.
    #7
    very useful thumb rule!

    and in the apple documentation, they highly recommend using delegates to dismiss modal views. Is it okay (good-practice wise) to use the [self dismissModalController] method?
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    They do? Where?
     
  9. macrumors 6502a

    Joined:
    Nov 9, 2007
    #9
    He is right, although I am on my iPad and cannot easily get a link to a reference right now. Modals are supposed to be dismissed by their parents.

    They do advise you use a delegate protocol to communicate between the modally presented controller and the controller that presented it (which would act as the modal controller's delegate) and have the parent dismiss it's child in response to a particular delegate call (e.g. someModalController:hasFinishedDoingSomething) to promote loose coupling.

    Apple use this design for some of their own components that are designed to be displayed modally (such as in-app mail, media library etc.).

    In really simple cases though, you can just call dismissModalViewController on the modal controller itself, and it will forward the call on to the parent automatically.
     
  10. macrumors member

    Joined:
    Sep 20, 2010
    Location:
    Somewhere but not here.
    #10
    From the Modal ViewController page

    "When it comes time to dismiss a modal view controller, the preferred approach is to let the parent view controller do the dismissing. In other words, the same view controller that presented the modal view controller should also take responsibility for dismissing it whenever possible. Although there are several techniques for notifying a parent view controller that it should dismiss its modally presented child, the preferred technique is delegation."
     
  11. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Thanks for that. It definitely seems to be a more recent addition but that doesn't excuse me for not being aware of it. :)
     

Share This Page