views:

176

answers:

3

View Controller A presents View Controller B modally, which has a button to present View Controller C modally.

Here is my flow:

A presents B which presents C

When B presents C, I want B to be dismissed, so my only view controllers are A and C. I am not sure where to call:

[self dismissModalViewControllerAnimated:NO];

in order to dismiss B.

I created a delegation pattern, where right after B presents C, A will dismiss B however nothing gets dismissed.

If B is C's delegate then C dismisses itself.

A: 

You should use a UINavigationController to do this.

*create a navigation controller with controller B as the root view controller. *A present the navigation controller as a modal with presentModalViewControler:animated: this will have the same affect as presenting B *When B needs to present C it pushes it ti the navigation view comptroller's stack by calling [self.navigationController pushViewController:C animated:YES] *if C needs to be dismissed to revile B you can do those by calling [self.navigationController popViewControllerAnimated:YES] *If C needs to dismiss and revile A you can dismiss the modal by calling [self.navigationController.parentViewController dismissModalAnimated:YES]

you could go 1 step further and not use a modal at all by embedding A as the root view controller of a navigation controller and pushing B to the Navigation controller instead of presenting it as a modal

Blueneon
I must present and dismiss modal view controllers because of the way my app is designed. The modal views are tabbarcontrollers which can only be displayed modally.
Sheehan Alam
It seems to me that there is a problem with the Apps design. You are fighting the framework, this is never a good idea and will lead to problems further down the line. However i think there may be a solution... A presents B normally. When B presents C do the following (assuming self is an instance of B):[self.parentViewController dismissModalViewControllerAnimated:NO];[self.parentViewController presentModalViewControler:C animated:YES];The first line may not be necessary.
Blueneon
A: 

I think that you can not do it. Read again the documentation for dimissing the modal view controller

If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

That means if you dismiss B, you will dismiss C as well

vodkhang
Thought: Is it possible to keep a reference to A, in B. Then have that reference of A, in B, present C? I can then dismiss B and continue showing C since it won't be dismissed by B?
Sheehan Alam
I tried the above, with no luck.
Sheehan Alam
I think you can not do that. The best way you can do is using some tricks: first using A present B, and then quickly dismiss B with no animation, and present C. I am not sure if that will work in your case
vodkhang
I tried dismissing B w/ no animation, and then presenting C, but I still get a crash.
Sheehan Alam
crash when dismissing B or when presenting C?
vodkhang
crash when presenting C
Sheehan Alam
I have opened a new question related to the presentation of C w/ more code: http://stackoverflow.com/questions/3224880/presentmodalviewcontroller-not-working-properly
Sheehan Alam
A: 

just do this from A

[self.navigationController dismissModalViewControllerAnimated:YES];
Fossli
Great tip. This is what I did to get it to work: C calls B which calls A which provides your code.
Sheehan Alam
you could do it from wher ever you want in a navigationcontroller-chain... but i really think you should consider who is in controll of what. children should not be aware of how it is implemented by it's parents..
Fossli