简体   繁体   中英

Dismiss presenting view controller

I have the following situation:

view controller A -> presents modally -> view controller B

view controller B -> presents modally -> view controller C

I would like to dismiss view controller "C" and go directly to "A" instead of showing "B" since it do not make sense to show "B" at that time.

How can I accomplish that?

Thanks, Daniel

In 'view controller C' ->

Before iOS 6: Use

[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];

Or,

[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES];

From and Above iOS 6: Use

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

You can use segues to present view controllers from other view controllers.

To go backwards through a hierarchies you normally dismiss presented view controllers or you go back to specific view controllers through unwind segues .

Try the following:

[vcB dismissModalViewControllerAnimated:NO]   //no Animation, so happens instantly
[vcA dismissModalViewControllerAnimated:YES]  //with Animation, this is all you see  

Your best bet would be use navigation controllers if your B and C are related and are more detailed version of A

A->BC . You unwindsegue to unwind the stack/pop the view controllers pushed on the stack.

Read this link very useful - http://chrisrisner.com/Unwinding-with-iOS-and-Storyboards

This is an old question and has an accepted answer but it did not solve my problem. Therefore, I would like to propose another solution for those who need different approaches.

The story is A presents B, B presents C, A is shown when C is dismissed. Here is my approach; Present ViewB from ViewA, when it comes to presenting ViewC from ViewB, do it from ViewA, and dismiss ViewB as follows;

let presentingNavCon = navigationController?.presentingViewController as? UINavigationController
let viewC = ViewC()
navigationController?.dismiss(animated: false)
presentingNavCon?.present(viewC, animated: true)

so, when you dismiss ViewC, ViewA appears.

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