2

I'm trying to find a proper way to handle my scene/view process flow in my game. I'm currently able to transition from a main menu to a game level, exit the game level and return to the main menu, but it's not deallocating any of the memory from any of the views.

I saw a lot of suggestions to use a UINavigationController to handle my view controllers. After trying that for a while, my current storyboard looks like this:

Main.storyboard

My root view controller is Menu View Controller. I first start in Title View Controller, segue to Menu View Controller, then segue to and from Game View Controller, but this is obviously not deallocating any of my memory.

My segues are done from within SKScenes using the following:

self.viewController?.performSegueWithIdentifier("segueName", sender: viewController)

where viewController is a var within the SKScene referencing its parent view controller.

Is there something I could try to get my process flow to work out? I just want to be able to traverse a title scene for login and server connections, then spend the rest of the time switching between a menu scene and some sort of game scene.

I've tried to implement a few suggestions, for example, dismissViewControllerAnimated(true, completion: nil) but none of them seem to be actually deallocating anything. The game slows to a crawl after just 2-3 cycles between the menu scene and game scene.

I've seen quite a few questions similar to this, but I've been so far unable to come up with a solution that works for me. I'm writing a game in Swift and Xcode 7.3.1.

Austin
  • 427
  • 2
  • 7
  • 16

2 Answers2

4

I solved my issue. I ended up having a bunch of strong references to various things like my view, scene, and view controller. I had ~200 strong references in total, so I have a lot of cleaning up to do.

To find my strong references, I followed a guide on how to use Allocation Instruments in Xcode here: https://www.raywenderlich.com/97886/instruments-tutorial-with-swift-getting-started

Anyone with this same issue, check out this question: In swift, how to get memory back to normal after an SKScene is removed?

That's what helped me fix my issue.

Community
  • 1
  • 1
Austin
  • 427
  • 2
  • 7
  • 16
0

So you've looked at a number of different approaches but most aren't appropriate for you and / or have been applied incorrectly.

You now have an unused navigation controller in your app. In general I would use a navigate controller, but it should be marked as the initial view controller and your current initial view controller should be the nav controllers root view controller.

Once you have that, you need to look into unwinding segues (or use a little code). At the moment you're just always showing new view controller instances, you're never going back. This is why you start having issues. You need to unwind from the game scene to the menu instead of showing a new menu.

That can also be done with code by telling the nav controller to pop the top view controller off its stack.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Hmmm. There was a point where I considered this, but it didn't sound right at the time, but now that I look at my process flow, a simple push/pop of game view controllers should work for me. I'll try an implementation of that and see what I can get working. – Austin Jul 08 '16 at 09:45
  • Perhaps I'm not using it correctly, but calling `self.navigationController?.popViewControllerAnimated(false)` from within gameViewController did not remove the game view or transition to menuViewController. Nothing happened. – Austin Jul 08 '16 at 13:53