-1

I am making a game in SpriteKit using Swift that currently has two viewControllers. The first viewController is the GameViewController that displays the menu with some buttons positioned in interface builder using storyboards. There is a play button on the menu that transitions to the second viewController that displays the gameScene. Both viewControllers load an SKScene.

To transition from one view controller to the other and back again i am using segues created in storyboard.

The problem i am having is that the memory increases when ever i switch between the viewControllers causing the app to eventually crash. I have a feeling the viewControllers are just stacking on each other. Here is my code for GameViewController and GameSceneViewController.

GameViewController:

class GameViewController: UIViewController, GKGameCenterControllerDelegate {

//MARK: Variables
var scene: Menu!
@IBOutlet weak var settingsBtn: UIButton!
@IBOutlet weak var leaderboardsBtn: UIButton!
@IBOutlet weak var storeBtn: UIButton!
@IBOutlet weak var playBtn: UIButton!

@IBAction func play(sender: UIButton) {


}

//MARK: Button Animations

func menuButtonAnimations(){

    settingsBtn.transform = CGAffineTransformMakeTranslation(0, 200)
    leaderboardsBtn.transform = CGAffineTransformMakeTranslation(0, 200)
    storeBtn.transform = CGAffineTransformMakeTranslation(0, 200)

    springWithDelay(0.5, 0.7, {
        self.settingsBtn.transform = CGAffineTransformMakeTranslation(0, 0)
    })
    springWithDelay(0.5, 0.75, {
        self.leaderboardsBtn.transform = CGAffineTransformMakeTranslation(0, 0)
    })
    springWithDelay(0.5, 0.8, {
        self.storeBtn.transform = CGAffineTransformMakeTranslation(0, 0)
    })

}

//MARK: The View
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    // Configure the view.
    let skView = self.view as SKView
    skView.showsFPS = false
    skView.showsNodeCount = false
    skView.showsPhysics = false

    skView.ignoresSiblingOrder = true

    scene = Menu(size: skView.bounds.size)
    scene.scaleMode = .AspectFill

    skView.presentScene(scene)

    //Button Animation
    menuButtonAnimations()

}

//MARK: Extra Stuff
override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override func prefersStatusBarHidden() -> Bool {
    return true
}
}

GameSceneViewController:

class GameSceneViewController: UIViewController{

    //MARK: Variables

    @IBOutlet weak var returnBtn: UIButton!

    //MARK: Helper Functions

    //MARK: The View
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        // Configure the view.
        var scene: GameScene!
        let skView = self.view as SKView
        skView.showsFPS = false
        skView.showsNodeCount = false
        skView.showsPhysics = false

        skView.ignoresSiblingOrder = true

        scene = GameScene(size: skView.bounds.size)
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)

        //Notifications
    }

The segues are both modal segues created using the linking in storyboard.

Any help on how to transition back and forth between the two viewControllers without constantly increasing the memory would be great!

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
PoisonedApps
  • 716
  • 8
  • 21

2 Answers2

1

If one view controller is presenting the other through a modal segue, you should use dismissViewControllerAnimated:completion: to return to the first one rather than stacking modal presentations.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • That depends on what's happening to make you want to go back to the first controller. If you have a button that connects to a segue, give it a method instead that calls the "dismiss". – Phillip Mills Dec 15 '14 at 19:10
1

I believe you can do this as an IBAction to dismiss the modal view:

Objective-C:

[self dismissViewControllerAnimated:YES completion:nil];

Swift:

self.dismissViewControllerAnimated(true, completion: nil)

(Dismiss Modal View)

And I believe that a back button can be linked to dismissController: from the containing NSViewController to dismiss a modal view (at least for Cocoa storyboard, anyways).

Community
  • 1
  • 1
droomph
  • 76
  • 4