UIViewController retaining the objects
I have a button on mainViewController, which on tap opens another view.
-(void)buttonTap:(id)sender
{
GameViewController *gameViewController = [[GameViewController alloc] initWithNibName:@"GameViewController" bundle:[NSBundle mainBundle]];
Level *level = [levels ob开发者_StackOverflowjectAtIndex:currentLevelNumber];
gameViewController.level = level;
UIView *gameview = gameViewController.view;
gameview.frame = CGRectMake(0, 0, 480, 320);
CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI/2.0);
[gameview setTransform:rotate];
gameview.center = CGPointMake(160, 240);
[[self.view window] addSubview:gameview];
[gameViewController release];
gameViewController = nil;
}
In the gameViewController I have another button to close itself.
-(IBAction)buttonTap:(id)sender
{
[self.view removeFromSuperview];
//[self.view release];
}
I have a timer in the gameViewController which still fires even after closing the view. The viewDidUnload or dealloc are not fired at all. Because of this the game eventually becomes choppy. When and how does this gameviewcontroller releases all the objects contained in it? I have been searching all over the internet but couldn't find the right answer.
When you add the view of a view controller, certain lifecycle methods on the view controller aren't called. These include things like the following:
viewWillAppear:
viewDidAppear:
viewWillDisappear:
viewDidDisappear:
viewDidUnload
may or may not be called. I believe it is if you ever explicitly set theview
property tonil
.- All rotation-related methods
Instead, you probably want to present a modal view controller. This formally adds it into a hierarchy of view controllers, helps manage your memory, and will invoke all of the methods above when appropriate. If you do it this way, you can release the view controller immediately after calling presentModalViewController:animated:
, and it will be kept around until its dismissal.
If you decide to stick with your current approach (though I don't recommend doing so), you should probably do the following when gameview
disappears from the screen:
gameViewController.view = nil;
[gameViewController release];
gameViewController = nil;
精彩评论