2014-12-02 28 views
1

我解釋我的問題。如何在viewController轉換期間釋放內存

我剛剛做了一個3觀點的遊戲。

  1. 遊戲演示用PLAYBUTTON
  2. 遊戲場景
  3. 比賽現場上的一個按鈕,再次播放或回到頒獎現場。

我的問題是當兩個場景之間存在轉換時,活動內存將增加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 
    }) 
} 

回答

0
instantiateViewControllerWithIdentifier("GameViewController") as UIViewController 

此代碼創建新的ViewController每次用戶按下按鈕。我建議你爲此使用singletone模式。

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

您還可以創建一個私有類方法,用於從故事板實例化視圖控制器。

+0

謝謝你的回答,但我是一個新的程序員(這是我的第一個應用程序)在哪裏添加你的代碼?你能解釋它理解嗎? – Haox 2014-12-02 22:23:59