2

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.

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

4 Answers4

1

I think you can use NSNotificationCenter. You can make a NSNotificationCenter at B that calling a method to dismiss itself, then make a post Notification from C to call the NSNotificationCenter at B.

I'm not sure of it, but it may works.

In B add:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissFunction:) name:@"DismissFunction" object:nil];

and the method like this:

- (void)dismissFunction:(NSNotification*)notification
{
      [self.navigationController dismissModalViewControllerAnimated:YES];
}

And in C add:

[[NSNotificationCenter defaultCenter] postNotificationName:@"DismissFunction" object:nil];
0

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
  • 26
  • 2
  • 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 Jul 11 '10 at 10:23
  • 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 Aug 02 '10 at 19:22
0

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
  • 18,639
  • 11
  • 76
  • 110
  • 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 Jul 11 '10 at 10:27
  • 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 Jul 12 '10 at 07:22
  • I tried dismissing B w/ no animation, and then presenting C, but I still get a crash. – Sheehan Alam Jul 13 '10 at 09:27
  • crash when dismissing B or when presenting C? – vodkhang Jul 13 '10 at 09:30
  • 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 Jul 13 '10 at 19:27
0

just do this from A

[self.navigationController dismissModalViewControllerAnimated:YES];
hfossli
  • 22,616
  • 10
  • 116
  • 130
  • Great tip. This is what I did to get it to work: C calls B which calls A which provides your code. – Sheehan Alam Jul 13 '10 at 09:45
  • 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.. – hfossli Jul 13 '10 at 11:07