Andre Cytryn
Andre Cytryn

Reputation: 2705

dismissModalViewController from another modalViewController

So I have a viewController A that presents a modal viewController B.

Then B presents a viewController C.

What I'm trying to do is to dismiss viewController B since I dont need it anymore. Is there a simple way to dismiss B and keep C on the screen as a child of A maybe?

Upvotes: 1

Views: 256

Answers (4)

NJones
NJones

Reputation: 27147

Obviously, as many have said, this in a textbook world would/should be a delegate situation. Where A presents B and B presents C. Then when C is done it tells B which dismisses C and then B tells A to dismiss B.

I would argue that you are essentially creating a set of modally presented view controllers that amounts to a navigation stack. I would likely implement it as such. Where A would be the root view controller. A would then push B onto the stack and B would push C onto the stack. When C was done it could simply popToRootViewControllerAnimated: or popToViewController:A animated:YES if A was not the rootViewController.

Also removing B out from underneath of C seems problematic. But it doesn't sound like that is set in stone based on your comment:

But anyway its not a the end of the world if I keep that viewController there for a while. –

That would at least enable a fairly clean delegate setup.

Still it seems like you basically know that when the user is done with C they would never need B again. If this is the case you can blindly dismiss two or more view controllers at once. And the code for two at a time is fairly simple. (assuming iOS version > 5.0)

[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES];

Please note that I said blindly! This code is not forgiving and makes assumptions. For example most obviously that the view controller does in fact have a presentingViewController and that that view controller has a presentingViewController. If either of these criteria is not met then this code will do nothing. This could easily happen if you at any point restructure your app.

So again, if you choose to use this line of code, use it very carefully. And please do consider using a UINavigationController for this view hierarchy, or at least delegation.

Upvotes: 2

tiguero
tiguero

Reputation: 11537

You could have a share instance that hold a weak pointer to your root view controller (so A here). Use this share instance to present / dismiss C whenever you want from A by using this pointer to A.

EDIT

This is to follow up the answer from TimD. In any case in the approach i proposed, C and B will both be presented from A.

Upvotes: 0

TimD
TimD

Reputation: 8522

You could try having A conform to a protocol that defines how C is presented, then have B call A to trigger the presentation of C, rather than B being responsible for presenting C itself. If C can exist independently of B, then A is effectively acting as the "parent" of both.

Upvotes: 1

David Hoerl
David Hoerl

Reputation: 41622

You might be able to make this work:

  • provide a property on B like "dieOnDismiss"

  • implement 'dismissViewControllerAnimated:YES' in B, and forward the message to super

  • C optionally sets that property

  • when C sends B 'dismissViewControllerAnimated:', B sends the dismiss to super, immediately sends A 'dismissViewControllerAnimated:NO'

Not sure if you can use animated on one transition and have it look right - you may be able to. But if not you can probably just return to A with no animation.

Upvotes: 1

Related Questions