iPhone - 多个UIViewControllers版本

时间:2009-09-22 00:04:31

标签: iphone memory uiview uiviewcontroller dealloc

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

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

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

相关问题