开发者

Trouble releasing view controller

My app is a question game. I have a view controller called Question_1; this is the ONLY view controller that gets loaded every question. I have an array set up in another class called HowToPlay. This array is loaded with strings, one representing each question. In the viewDidLoad method of Question_1, it randomly picks a question, and then changes the text of the question. After the user presses the next button, it reloads itself, until it realizes there is nothing left in the array to load then it loads the game over controller.

Here is how I'm loading my nib file to reload the question:

[background release];
[menu release]开发者_如何学JAVA;
[changeOnWrong release];
[callPauseLabels release];
[youArePaused release];
[only1Pause release];
[resume release];
[afterPause release];
[pause release];
[timerString release];
[timerLabel release];
[trueButton release];
[falseButton release];
[continueButton release];
[showYesNo release];
[scoreAmmount release];


callPauseLabels = nil;
afterPause = nil;
countDown = nil;
Question_1 *Q1 = [[Question_1 alloc] initWithNibName:@"Question 1" bundle:nil];

Q1.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:Q1 animated:YES];

[Q1 release];
Q1 = nil;

The problem I'm having is that each time this view gets reloaded, it doesn't release the old one, and keeps building in memory. It's like each view controller keeps getting loaded on top of the other. I need it to release the old view controller. I have tried things like this:

[self dismissModalViewControllerAnimated:NO];
[self.parentViewController dismissModalViewControllerAnimated:NO];

It still doesn't release the old view from memory.


If you're going to display this view controller over and over, there's no reason to throw it away and load it again. Just hold onto it in an ivar, and reconfigure it for each question.

But in answer to your question, you'll need something "like"

[self.parentViewController dismissModalViewControllerAnimated:NO];

But if you're not using a navigation controller or tab bar controller, parentViewController won't be set. You'll need to set it yourself (that may not always be safe; I never do it that way), or set another variable to keep track of your parent (I usually use a delegate). Then you would do this:

[self.delegate dismissModalViewControllerAnimated:NO];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜