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.

3
  • Do you want to dismiss both controllers? In the code you have provided you are dismissing both controllers on button click. Commented Sep 26, 2016 at 10:19
  • No only the first view, once the segue has completed and the second view is on top of it, with the first view underneath. Commented Sep 26, 2016 at 10:21
  • Why do you want to do that ? actual purpose ? Commented Sep 26, 2016 at 11:43

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

9 Comments

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 <TimerAndViewControllerDismiss.HomeViewController: 0x7b4abbd0> on <TimerAndViewControllerDismiss.SectionOneViewController: 0x7b12b3e0> whose view is not in the window hierarchy!
If you have dismissed 1st view controller, how will you go back to it? I have edited my answer have a look.
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.
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
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.
|
0

Refer to these links:

  1. Navigation Controller

https://developer.apple.com/reference/uikit/uinavigationcontroller

  1. Presenting a controller

https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/PresentingaViewController.html#//apple_ref/doc/uid/TP40007457-CH14-SW1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.