简体   繁体   中英

Dismiss all UIViewController

I have app with 3 UIViewContoller

UIViewContollerA
UIViewContollerB
UIViewContollerC

UIViewContollerA open UIViewContollerB with presentModalViewController UIViewContollerB open UIViewContollerC with presentModalViewController

And i want to have the possible that in one click on button in UIViewContollerC to dismiss all The UIViewController.

I try to call dismissModalViewControllerAnimated twice but it won't work, there is any other option to do it? it's possible?

I would take a look at unwind segues. They allow you to unwind to any previous segue. The answers in this question are very helpful in describing how to use them: What are Unwind segues for and how do you use them?

Also as mentioned by Michael, modals weren't designed to be placed one on top of the other. I've found they work great for forms, but not much else. I would suggest that you use a UINavigation Controller as well. This will allow you to easily navigate back to the previous controller. Then you could also add a button for an unwind segue back to the initial view controller that you want.

You probably need some sort of chaining set of calls back up the modal stack to do this.

However, given you have a stack of view controllers, a UINavigationController might be a simpler solution to both presenting these view controllers in the first place and dismissing them down in one go.

Of course calling dismissModalViewControllerAnimated won't work, it's intended to be called on different view controllers. From the docs:

Dismisses the view controller that was presented by the receiver

So you'll need to set up delegation between the controllers in order to pass a request to dismiss the controller backwards to the initial controller.

Keep in mind that:

  1. There's probably a better way of doing whatever it is you're trying to do. Modal controllers weren't intended to be presented on top of each other.
  2. You're using deprecated methods. Use presentViewController and dismissViewController instead

you would pass a reference of UIViewContollerB in UIViewContollerC , like this

@property(nonatomic, assign) UIViewContollerB *refB;

and call dismissModalViewControllerAnimated in UIViewContollerC to dismiss it, and fire dismiss action of UIViewContollerB in viewWillDisappear

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.refB dismissModalViewControllerAnimated:YES]
}

What you could try is to use dismissModalViewControllerAnimated with animated:NO . On the top most one and animated:YES on the controller below.

Or perhaps it workd, if you just call dismiss on the rootController?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM