0

How would I go about dismissing a view controller once a segue has been performed? Once the new view controller has animated on top, I want to dismiss the view controller underneath (the one which the segue was initially triggered from).

I tried the following code but I am getting issues with views not being in the heirarchy.

@IBAction func gotoSection1(_ sender: AnyObject) {
    let presentingViewController: UIViewController! = self.presentingViewController

    self.dismiss(animated: false) {
        presentingViewController.dismiss(animated: false, completion: nil)
    }
}

Any help would be greatly apprceiated.

user1391152
  • 1,259
  • 3
  • 13
  • 28

2 Answers2

1

Try this:

Add the below code to first view controller.

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        self.dismiss(animated: true, completion: nil)
    }

It will dismiss the first view controller before presenting second view controller on top of it.

Edit:

Follow these steps and check:

  1. Create a button in 1st View controller
  2. Connect button to 2nd View controller with modal segue
  3. Implement prepareForSegue in 1st view controller
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • I get the same error. I go from page one to page two. When I click a button to go back to page one i get the following error arning: Attempt to present on whose view is not in the window hierarchy! – user1391152 Sep 26 '16 at 11:38
  • If you have dismissed 1st view controller, how will you go back to it? I have edited my answer have a look. – PGDev Sep 26 '16 at 11:41
  • If you want to go back to 1st view controller, don't dismiss it. When you dismiss 2nd view controller, you will automatically land on 1st view controller. – PGDev Sep 26 '16 at 11:43
  • so if i keep going between 2 view controllers without dismissing, will I not have multiple copies of each just stacked on top of each other? Basically I will have around 10 different view controllers, each with a button that will go back to VC1. What I am trying to avoid is having multiple copies of the same view stacked on top of each other and slowing the app down – user1391152 Sep 26 '16 at 11:49
  • You won't have multiple copies. 1st view controller will be created only once. 2nd view controller is created each time when we tap he button on 1st view controller and 2nd controller is deallocated each time it is dismissed. So you won't create multiple copies of the controller. – PGDev Sep 26 '16 at 11:54
  • so if i went to VC1 from VC5 it would take VC1 from the bottom of the stack and place it on top? – user1391152 Sep 26 '16 at 12:02
  • In a navigation stack the controllers are stacked on top of each other i.e. VC1 -> VC2 -> VC3 and so on. If you want to move from VC1 to VC5, VC1 must be on the top of the navigation stack. All other controllers i.e. VC2, VC3, VC4 does not exist. Nothing is picked up and placed on the top. The thing you asked is presenting a controller modally. In this case there is nothing like a stack. – PGDev Sep 26 '16 at 12:09
  • Read this : http://stackoverflow.com/questions/20742745/navigation-controller-push-view-controller – PGDev Sep 26 '16 at 12:10
  • Thanks for your time and help. Technically the answer you gave is correct to the original question, so I'll mark it as the correct answer. – user1391152 Sep 26 '16 at 14:08