I always thought that the OS dealt with memory when it comes to using viewControllers.
At the moment I have 3 views, the first is of course loaded into the Navigation Controller at startup and I have a button which when tapped adds a subview, on the subview is another button and once tapped removes the subview and then allocates a viewController and pops this new view onto the stack. The new view has a back button and when tapped pops the view.
It all works fine until I continually tap from the subview to add the viewController and back again, eventually it crashes but I don't really know why. I am releasing my objects and as I said I thought the OS would deal with it if memory was getting low.
Here's my code when the subview button is tapped to allocate the viewController:
Code:
-(IBAction) twoPlayerButton :(id)sender
{
newTimer = [NSTimer scheduledTimerWithTimeInterval:0.40 target:self selector:@selector(show2PlayerScreen) userInfo:nil repeats:NO];
NSString *path = [[NSBundle mainBundle] pathForResource:@"ButtonPress" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.buttonPress = theAudio;
[buttonPress play];
[theAudio release];
white2PlayerGameFlash.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"White2PlayerGame.png"],
[UIImage imageNamed:@"WhiteBG.png"],
[UIImage imageNamed:@"White2PlayerGame.png"],
[UIImage imageNamed:@"WhiteBG.png"],
[UIImage imageNamed:@"White2PlayerGame.png"],
nil];
white2PlayerGameFlash.animationDuration = 0.35;
white2PlayerGameFlash.animationRepeatCount = 1;
[white2PlayerGameFlash startAnimating];
}
And this is the code which fires the timer and adds the viewController:
Code:
-(void) show2PlayerScreen
{
UINavigationController *navController = self.navigationController;
TwoPlayerGame *twoPlayerGame = [[TwoPlayerGame alloc] initWithNibName:@"TwoPlayerGame" bundle:Nil];;
[navController pushViewController:twoPlayerGame animated:NO];
[twoPlayerGame release];
Here's what the console is telling me:
Code:
2010-10-25 20:24:50.537 LightsOut[349:207] Received memory warning. Level=1
2010-10-25 20:25:06.883 LightsOut[349:207] Received memory warning. Level=1
2010-10-25 20:25:19.809 LightsOut[349:207] Received memory warning. Level=1
2010-10-25 20:25:21.534 LightsOut[349:207] Received memory warning. Level=1
2010-10-25 20:25:25.712 LightsOut[349:207] Received memory warning. Level=2
Program received signal: “0”.
So as soon as it receives memory warning level 2 the app crashes. Can anyone advise me as to how I might resolve this or what I'm doing wrong? Thanks.
Oh I'm running this on the iPad, not sure if that is relevant!