PDA

View Full Version : dismissModalViewController Not Working




stadidas
Dec 27, 2008, 12:34 PM
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:


transactionViewController = [[TransactionViewController alloc] initWithNibName:@"TransactionView" bundle:nil];


I then present this as a modal dialogue like this:


[[self navigationController] pushViewController:transactionViewController animated:YES];


To dismiss this modal view, from TransactionViewController I do this:


[[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.



drivefast
Dec 27, 2008, 12:47 PM
(deleted duplicate)

drivefast
Dec 27, 2008, 12:48 PM
try
[self dismissModalViewControllerAnimated:YES];

the dismiss also works on the modal view controller itself.

stadidas
Dec 27, 2008, 12:50 PM
Excellent! Thank you good sir :)

admanimal
Dec 27, 2008, 03:39 PM
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.

dejo
Dec 27, 2008, 04:59 PM
popViewControllerAnimated when you used pushViewController:animated:

dismissModalViewControllerAnimated when you used presentModalViewController:animated:

kingthong
Oct 29, 2010, 04:17 AM
popViewControllerAnimated when you used pushViewController:animated:

dismissModalViewControllerAnimated when you used presentModalViewController:animated:

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?

dejo
Oct 29, 2010, 06:16 AM
and in the apple documentation, they highly recommend using delegates to dismiss modal views.
They do? Where?

Luke Redpath
Oct 29, 2010, 11:21 AM
They do? Where?

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.

kingthong
Nov 2, 2010, 12:36 AM
They do? Where?

From the Modal ViewController (http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW3) 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."

dejo
Nov 2, 2010, 04:38 PM
From the Modal ViewController (http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW3) 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."

Thanks for that. It definitely seems to be a more recent addition but that doesn't excuse me for not being aware of it. :)