0

我的主要 UIViewController (PMGameViewController.h) 是我的应用程序委托调用的文件。

我的主 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];
    }
4

2 回答 2

1

我不知道你为什么要这样做,因为之后你可能需要你的 PGGameViewController。但如果你真的想释放它,你可以这样做:

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

反向引用它,然后做你的东西并在你不需要它时释放它:

[tmpViewController release]

如果您必须保留引用一段时间,您可以在两个游戏视图控制器中创建一个 id ivar,并使用 asign 协议,但不要忘记在释放控制器后将其设置为 nil :

id tmpViewController;
...
@property (nonatomic, assign) id tmpViewController;
...
@synthesize tmpViewController;
于 2010-01-31T02:19:48.480 回答
0

删除后可以将视图控制器设置为 nil 。在将其设置为 nil 之前,您可以选择释放它。是否发布它取决于使用情况和加载成本。

于 2009-09-22T04:06:41.190 回答