I am making a game, where I go from the main screen to a ArcadeViewController, which loads up the SKScene, and save the previous VC as prevVC.

I use segues created in storyboard to move between VC.

The problem is that each time I move to a VC, instead of moving into the old one, a copy gets created, and both of them start to run at the same time.

I tried removing them by running the following codes, when I move into the VC:

        override func viewDidAppear(_ animated: Bool) {
    UIApplication.shared.keyWindow?.rootViewController = self
    self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
    prevVC.dismiss(animated: false, completion: nil)
     UserDefaults.standard.set(0, forKey: "since_The_Last_Ad")

   extension UIViewController {
func  reloadViewFromnib() {
    let parent = view.superview
    view = nil
    parent?.addSubview(view) // This line causes the view to be reloaded

It helped to reduce the number of copies created, but still the are some.

How can I remove duplicate views?

1 Answers

Oblivion Creature On Best Solutions

So the solution ended up being to use the normal segue to launch the ArcadeVC from GameVC(start VC) for the first time. Then set the ArcadeVC as rootVC:

   UIApplication.shared.keyWindow?.rootViewController = self

Then use a normal segue to go back to the GameVC. After the ArcadeVC is set as the rootVC, just use normal segue to the ArcadeVC form GameVC, and unwind from GameVC to AracdeVC.