views:

543

answers:

2

My main UIViewController, (PMGameViewController.h), is the file which my apps delegate calls.

There are several buttons on my main UIViewController (PMGameViewController.m). When a button is pressed I do an insertSuvbiew and attach another UIViewController on top. When the mini game is over I simply do a removeFromSubview. This removes the UIViewController I inserted on top and shows me the main menu. Perfect this is what I want, but...

After I do a removeFromSubview, the objectalloc does not drop. How can I release that UIViewController's memory. I do not know of a way to backreference to my main UIViewController (PMGameViewController.m) to tell it that it has been removed and to release the UIViewController memory.

Here is how I insert the Subview

//////////////////////////////////////
//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];
    }
A: 

You can set the view controller to nil after you remove it . Prior to setting it to nil you can optionally release it. Whether you release it or not depends on use and how expensive it is to load.

Jordan
+1  A: 

I'm don't know why you want to do this, since you might need your PGGameViewController afterwards. But if you really want to release it, you could do this :

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

to backreference it, then do your stuff and release it when you don't need it :

[tmpViewController release]

If you have to keep the reference for a while, you could create an id ivar in your two games view controllers, and use an asign protocol, but don't forget to set it to nil after releasing the controller :

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