0
votes

I have an iOS app consists of three view controllers

homeViewController -> newGameSelectionViewController -> GameViewController

In GameViewController, I have a "home" button that should close the view controller and return to the newGameSelectionViewController.

In newGameSelectionViewController,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    gameVC = segue.destinationViewController as! GameViewController
    gameVC.countdown = 60
}

Then, I created an unwind segue to newGameSelectionViewController from GameViewController.

In newGameSelectionViewController,

@IBAction func unwindFromHomeButton(segue: UIStoryboardSegue) {
    gameVC.dismissViewControllerAnimated(true, completion: nil)
}

When I press the home button in GameViewController, view closes and returns to the newGameSelectionViewController

The problem is, after pressing the home button and returning to the newGameSelectionViewController, countdown timer in GameView controller still continues. If I open an another GameViewController, it also open a completely new view controller. In another words, first GameViewController did not close by calling dismissViewController(). How do I close the ViewController completely so that if I start a new game, previous view controller does not continue counting at the background.

I am printing the countdown values with println() in GameViewController. Thats where I see the previous countdown values are still printing after dismissViewController()

1
You should disable timer in viewWillDisappear if you want to stop it as soon as it is about to disappear or in viewDidDisappear to make sure the view is gone when you disable timer.Omar

1 Answers

1
votes

Your gameVC variable will be holding a strong reference to the GameViewController instance, so even though it has been dismissed, the object still exists as its reference count is not 0.

You need to set gameVC to nil in your unwind method in order to remove this reference.

@IBAction func unwindFromHomeButton(segue: UIStoryboardSegue) {
    gameVC=nil;
}

You don't need to dismiss the view controller explicitly in the unwind method if you are correctly invoking it via a segue - The unwind process will do this for you.