1

I have one ViewController, I switch to it programmatically from an tableviewcell with the following function.

private func switchView(identifier : String){
    DispatchQueue.main.async {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: identifier) as UIViewController
        self.present(vc, animated: true, completion: nil)
     }
}

After a while I need to switch back to the original state, so I call switchView with the same view again. The only problem is that a lot of stuff stays active in the previous ViewController.

How can I dismiss the previous ViewController with all of its contents?

G.Dimov
  • 2,173
  • 3
  • 15
  • 40
da1lbi3
  • 4,369
  • 6
  • 31
  • 65
  • When you want to go to previous view just check identifier and use pushviewcontroller instead of present. – teja_D Jul 22 '19 at 10:25

1 Answers1

0

Have you tried calling 'dismiss' on the VC that you are trying to get rid of?

self.dismiss(animated: true, completion: {
let vc = FirstController()
pvc?.present(vc, animated: true, completion: nil)
})

Found this answer here

L Schaick
  • 16
  • 6
  • should be noted that this code will go back to `FirstController` and then present `FirstController` on top of itself. causing the user to have 2x `FirstController` in the viewController stack. – Vollan Jul 22 '19 at 08:56
  • @Vollan That is not what I want. I want to navigate back from FirstController to a new Fresh FirstController. – da1lbi3 Jul 22 '19 at 10:30