Dismissing Modal View Controllers

2019-02-26 18:26发布

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.

4条回答
戒情不戒烟
2楼-- · 2019-02-26 19:00

just do this from A

[self.navigationController dismissModalViewControllerAnimated:YES];
查看更多
【Aperson】
3楼-- · 2019-02-26 19:03

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

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-02-26 19:18

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];
查看更多
Juvenile、少年°
5楼-- · 2019-02-26 19:22

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

查看更多
登录 后发表回答