Dismiss only presented view controller not the whole navigation stack Swift

983 Views Asked by At

I have a tab bar controller. In that the first tab is a navigation controller. Lets call it controller A. I am then pushing another view controller on it. Lets call it controller B. After that I am presenting view controller C from View controller B. Now I want to dismiss only the view controller B.

Tab Bar - A(Navigation Controller's root vc) -> Push VC -> B -> Present VC -> C

A to B is going using self.navigationController.pushViewController(animated: true, completion: nil)

B to C is going like this
let vc = CViewController() vc.modalPresentationStyle = .fullScreen self.present(vc,animated: true,completion: nil)

Now When I use self.dismiss(animated: true, completion: nil) in View Controller C. It goes back to the root view controller i.e vc A. I want it to go to VC B.

Video of issue

1

There are 1 best solutions below

2
Orion Cygnus On BEST ANSWER

After a bit of thought, I replicated what you were trying to do and figured out the problem is not with calling dismiss. It is with the way you called that View Controller in the first place. Change your "B to C" code a little bit.

Instead of:

let vc = CViewController()

vc.modalPresentationStyle = .fullScreen

self.present(vc,animated: true,completion: nil)

use:

let sb : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewController(identifier: "C")
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)

You would have to specify the identifier of your view controller in the storyboard, (Storyboard ID)

Now when you call self.dismiss(), it should only close C. I have tested this on my computer with Xcode 11.1.