iPhone - 多个UIViewControllers版本
问题描述:
我的主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];
}
答
您可以设置视图控制器零删除后。在将其设置为零之前,您可以选择将其释放。无论您是否释放它,取决于使用情况以及它的加载成本。
答
我不知道你为什么要这样做,因为之后你可能需要你的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;
你不不喜欢我们的答案?没有意见 ? – Julien 2010-02-01 18:37:53