如何在viewController转换期间释放内存

时间:2014-12-02 21:42:36

标签: ios iphone memory swift sprite-kit

我解释了我的问题。

我刚开了一个有3个视图的游戏。

  1. 带有播放按钮的游戏演示
  2. 游戏场景
  3. 场景中的游戏,可以再次播放按钮或返回演示场景。
  4. 我的问题是,当两个场景之间存在转换时,活动内存将增加30MB,每个场景转换将使活动内存增加约30MB,并且永远不会减少。

    如何修复它并释放内存?

    谢谢大家

    我的转换代码:

    // Transition in presentationViewController file
    func transition(sender:UIButton!)
    {
    
        println("transition")
        let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("GameViewController") as UIViewController
    
        let window = UIApplication.sharedApplication().windows[0] as UIWindow
        UIView.transitionFromView(
            window.rootViewController!.view,
            toView: secondViewController.view,
            duration: 0.65,
            options: .TransitionCrossDissolve,
            completion: {
                finished in window.rootViewController = secondViewController
        })
    }
    
    
    // transition in GameScene file
    
    func removeCountDownTimerView()
    {
    
        defaults.setInteger(balloonDestroyed, forKey: "score")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let gameOverScene: UIViewController = storyboard.instantiateViewControllerWithIdentifier("GameOverViewController") as UIViewController
        let vc = self.view?.window?.rootViewController
        vc?.presentViewController(gameOverScene, animated: true, completion: nil)
    
    
    }
    
    // transition in gameOverViewController file
    
    func transition(sender:UIButton!)
    {
        println("play transition")
        let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("GameViewController") as UIViewController
    
    
        let window = UIApplication.sharedApplication().windows[0] as UIWindow
        UIView.transitionFromView(
            window.rootViewController!.view,
            toView: secondViewController.view,
            duration: 0.65,
            options: .TransitionCrossDissolve,
            completion: {
                finished in window.rootViewController = secondViewController
        })
    }
    

1 个答案:

答案 0 :(得分:0)

instantiateViewControllerWithIdentifier("GameViewController") as UIViewController

每次用户按下按钮时,此代码都会创建新的ViewController。我建议你使用单音模式。

private let _SomeManagerSharedInstance = GameViewController() 
class GameOverViewController {
class var sharedInstance: GameOverViewController {
       return _GameOverViewController
}

您还可以创建一个私有类方法,用于从故事板中实例化View Controller。