-3

I made 4 ViewControllers (A, B, C, D) and want to dismiss D and C to back to B from D.
I used presentingViewController?.presentingViewController?.dismiss(animated: false, completion: nil) in D, but it didn't look dismissing simultaneously even I set animated: false. It looked like, at first dismiss D and moved to C, and the second dismiss C and moved to B. I don't want to show C during the transition.
How can I solve it?

the flow
A -> B -> C -> D -> B (dismiss D and C)

rmaddy
  • 314,917
  • 42
  • 532
  • 579
K.K.D
  • 917
  • 1
  • 12
  • 27
  • As for as I'm aware it's not possible to do this at the same time. You'll have to do them one at a time. Since the Navigation Controller acts like a stack when VCs are presented. So this would be LIFO in terms of a popping order. Id recommend doing them one at a time and try triggering the second one in the completion block of the first. Otherwise you may have to reset your navigation controller's stack and manually segue to the desired one like so: https://stackoverflow.com/a/41042191/6448167. Hope that helps. – KSigWyatt Dec 19 '18 at 01:14
  • If you're using storyboards, you might consider an unwind segue. [Technical Note TN2298: Using Unwind Segues](https://developer.apple.com/library/archive/technotes/tn2298/_index.html). – Rob Dec 19 '18 at 01:29
  • 2
    Did A, B, and C just present B, C, and D, respectively (i.e. not push segue or anything other than simple `present`/`show`)? Are you doing any animation related stuff in any of the appearance/disappearance methods in any of these view controllers? Any custom transitions or the like? While unwind segues are perfect for this sort of thing, calling `dismiss` on the appropriate view controller, like you have shown us above, should work fine, and I suspect something else is going on. Also, any other controllers involved here (e.g. `UISearchController` can cause problems if you don't use it right)? – Rob Dec 19 '18 at 01:41
  • Create blank project and figure out the minimum required to reproduce the problem. See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), a MCVE. – Rob Dec 19 '18 at 01:42

1 Answers1

0

Use self.navigationController?.popToViewController

Sample code :

if let firstVC = self.navigationController?.viewControllers[0]{
    self.navigationController?.popToViewController(firstVC, animated: false)
}

Here from viewcontroller "D" you can jump to any other viewcontroller located lower in the navigation stack.

While it is possible to jump to any viewcontroller in the navigation stack, it might be be advisable for a user experience stand point. The user would have in their mind a navigation work flow (A -> B -> C -> D), based on some action if we abruptly jump to another view controller, it confuse the user.

TheAppMentor
  • 1,091
  • 7
  • 14
  • This obviously assumes that he’s using navigation controllers rather than presenting/showing view controllers... – Rob Dec 19 '18 at 20:48
  • Yes, I am looking for the way to use presenting/showing view controllers...But I understood that using navigation controller is the best way for this problem. Thanks! – K.K.D Dec 20 '18 at 05:12