2009-09-22 27 views
0

我的主UIViewController(PMGameViewController.h)是我的應用程序委託調用的文件。iPhone - 多個UIViewControllers版本

我的主UIViewController(PMGameViewController.m)上有幾個按鈕。當按下按鈕時,我執行一個insertSuvbiew,並將另一個UIViewController附加在頂部。當迷你遊戲結束時,我只需執行removeFromSubview。這將刪除頂部插入的UIViewController,並向我顯示主菜單。完美這是我想要的,但...

我做了removeFromSubview後,objectalloc不會下降。我如何釋放UIViewController的內存。我不知道如何反向引用我的主UIViewController(PMGameViewController.m),告訴它它已被刪除並釋放UIViewController內存。

這裏是我插入子視圖

////////////////////////////////////// 
//Buttons are in PMGameViewController.m file 
////////////////////////////////////// 

if((UIButton *) sender == gameClassicBtn) { 
     ////////////////////////////////////// 
     //This Inserts the GameClassic.h file 
     ////////////////////////////////////// 
     GameClassic *gameClassicController = [[GameClassic alloc] 
              initWithNibName:@"GameClassic" bundle:nil]; 
     self.gameClassic = gameClassicController; 
     [gameClassicController release]; 
     [self.view insertSubview:gameClassicController.view atIndex:1]; 
    } 

if((UIButton *) sender == gameArcadeBtn) { 
     ////////////////////////////////////// 
     //This Inserts the GameArcade.h file 
     ////////////////////////////////////// 
     GameArcade *gameArcadeController = [[GameArcade alloc] 
              initWithNibName:@"GameArcade" bundle:nil]; 
     self.gameArcade = gameArcadeController; 
     [gameArcadeController release]; 
     [self.view insertSubview:gameArcadeController.view atIndex:1]; 
    } 
+0

你不不喜歡我們的答案?沒有意見 ? – Julien 2010-02-01 18:37:53

回答

0

您可以設置視圖控制器零刪除後。在將其設置爲零之前,您可以選擇將其釋放。無論您是否釋放它,取決於使用情況以及它的加載成本。

1

我不知道你爲什麼要這樣做,因爲之後你可能需要你的PGGameViewController。但如果你真的想要釋放它,你可以這樣做:

PMGameViewController *tmpViewController = [[[UIApplication sharedApplication] delegate] viewController(or however it's called)] 

到反向引用它,然後做你的東西,並釋放它,當你不需要它:

[tmpViewController release] 

如果你有保持了一段時間的參考,你可以在你的兩場比賽視圖控制器創建一個id伊娃,並使用ASIGN協議,但不要忘記釋放控制器後,將其設置爲nil:

id tmpViewController; 
... 
@property (nonatomic, assign) id tmpViewController; 
... 
@synthesize tmpViewController;