i have 3 view controllers. VC1(initial & home screen), VC2(SKScene & game), VC3(UIViewController & Gameover screen). My problem is when i start with VC1, then go to VC2 and then move to VC3 and then back to VC1, they all stack up on top of each other so it looks like this. VC1 - VC2 - VC3 - VC1 - VC2 - VC3 - VC1 - VC2 - VC3 - VC1 - VC2 - VC3, so eventually the memory builds up and the game slows down drastically eventually crashing. I need to somehow release VC1 when i move to VC2, then release VC2 when moving to VC3, etc. Everytime i make a loop around the VCs i want them to restart as if they were never there before. I will provide code of how i transfer between all of them. btw using sprite kit in xcode5. VC = View Controller
Inside VC1:
@implementation HomeScreen
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)goToGame:(id)sender {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"GameScreen"];
[self presentViewController:vc animated:YES completion:nil];
} //This moves me to my VC2
Inside VC2:
@implementation ViewController
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentMyViewController) name:@"presentMyViewController" object:nil];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
-(void)presentMyViewController
{
[self performSegueWithIdentifier:@"Segue" sender:nil];
}
Inside SKScene which is connected to VC2:
@implementation
{
int _lives;
BOOL _gameOver;
}
- (void)update:(NSTimeInterval)currentTime
{
if (_lives == 0 && !_gameOver) {
_gameOver = YES;
NSLog(@"You Lose!");
[self presentVC3];
}
-(void)presentVC3
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"presentMyViewController" object:nil];
}// This moves me to my VC3
Inside VC3:
@implementation ViewController3
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)goToHomeScreen:(id)sender {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"HomeScreen"];
[self presentViewController:vc animated:YES completion:nil];
} // This move me back to VC1
VC1 is a UIViewController, VC2 is a ViewController that presents an SKScene which is my actual game, and VC3 is a UIViewController.
So just to restate. As i move from VC to VC they all overlap onto one another increasing the memory and CPU and decreasing the FPS(frames per second) of the game until eventually crashing. I was told i need to dismiss my VC or release them from memory to fix this problem, if that is the correct solution then how do i go about doing that? If you have the correct solution please provide some code to help. Any help is appreciated!