Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
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.
 
try
Code:
[self dismissModalViewControllerAnimated:YES];

the dismiss also works on the modal view controller itself.
 
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.
 
popViewControllerAnimated when you used pushViewController:animated:

dismissModalViewControllerAnimated when you used presentModalViewController:animated:
 
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?
 
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.
 
They do? Where?

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

Thanks for that. It definitely seems to be a more recent addition but that doesn't excuse me for not being aware of it. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.