iPhone-多个UIViewController发布
我的主UIViewController(PMGameViewController.h)是我的应用程序代理调用的文件 在我的主UIViewController(PMGameViewController.m)上有几个按钮。当按下一个按钮时,我会执行InsertSuvView并在顶部附加另一个UIViewController。小游戏结束后,我只需执行removeFromSubview。这将删除我插入顶部的UIViewController,并显示主菜单。完美这是我想要的,但是 在执行removeFromSubview之后,objectalloc不会删除。如何释放UIViewController的内存。我不知道有什么方法可以反向引用我的主UIViewController(PMGameViewController.m),告诉它它已被删除并释放UIViewController内存 下面是插入子视图的方法iPhone-多个UIViewController发布,iphone,memory,uiview,uiviewcontroller,dealloc,Iphone,Memory,Uiview,Uiviewcontroller,Dealloc,我的主UIViewController(PMGameViewController.h)是我的应用程序代理调用的文件 在我的主UIViewController(PMGameViewController.m)上有几个按钮。当按下一个按钮时,我会执行InsertSuvView并在顶部附加另一个UIViewController。小游戏结束后,我只需执行removeFromSubview。这将删除我插入顶部的UIViewController,并显示主菜单。完美这是我想要的,但是 在执行removeFrom
//////////////////////////////////////
//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];
}
删除视图控制器后,可以将其设置为零。在将其设置为nil之前,您可以选择释放它。您是否发布它取决于使用情况和加载成本。我不知道您为什么要这样做,因为您可能需要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;
你不喜欢我们的答案?无可奉告?