title: How To Navigate Objective-C App Programmatically Without Using Only Storyboard?
body: <p>I am having trouble navigating my application. I have written a game scene completely in Sprite Kit (all buttons, etc. using SKNodes) with Objective-C. When the user opens the app, it presents a main menu which is built completely in Storyboard and contains a UIButton that triggers a transition to the ViewController with my game scene in it. </p>
  
  <p>Originally, to restart the game once the user lost I just presented a new scene like this:</p>
  
  <pre><code>GameScene *newScene = [GameScene sceneWithSize:self.view.bounds.size];
  newScene.anchorPoint = CGPointMake(0.5, 0.5);
  [self.view presentScene:newScene transition:[SKTransition fadeWithDuration:1]];
  [self removeAllChildren];
  [newScene sceneSetupWithSize:screenSize];
  </code></pre>
  
  <p>Where <code>sceneSetupWithSize:(CGSize)size</code> is a custom method I am using to set up my game. Unfortunately, this new scene always has the same incorrect size no matter what value I send. This procedure also does not enable me to navigate back to the main menu.</p>
  
  <p>To fix this, I resolved to just call the scene's ViewController. I did this by creating a ViewController object in the game scene class:</p>
  
  <pre><code>@property (nonatomic) GameViewController *currentController;
  </code></pre>
  
  <p>And then assigning the controller when the ViewController is loading the scene:</p>
  
  <pre><code>sceneNode.currentController = self;
  </code></pre>
  
  <p>I then use that object to call a custom method in the ViewController from the game scene class like this:</p>
  
  <pre><code>// In Game Scene Class
  [self removeAllChildren];
  [self removeAllActions];
  [_currentController restart];
  
  // In ViewController Class
  - (void)restart{
       NSLog(@"Restarting Scene");
  
       NSString * storyboardName = @"Main";
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
       UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"GameView"];
       vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
       [self presentViewController:vc animated:YES completion:nil];
  }
  </code></pre>
  
  <p>This also allows me to navigate to the main menu just by changing the identifier. It works like a charm except for one problem: it never pops the old ViewControllers from the stack. So when running the app, every time I navigate to the next ViewController it adds ~40MB to the processing memory. This means that every time the user loses and restarts the game, it takes up more and more memory. I attempted to use this instead:</p>
  
  <pre><code>[self.navigationController pushViewController:vc animated:YES];
  </code></pre>
  
  <p>But it just gives me a blank screen. I even tried to pop the current controller afterwards like this:</p>
  
  <pre><code>[self.navigationController popViewControllerAnimated:YES];
  </code></pre>
  
  <p>But nothing worked. I'm assuming that <code>self.navigationController</code> is <code>nil</code>, but I don't know what to do with that. I would prefer not to use any UIButtons in the game scene. I would also prefer to use the game scene as is without needing to recreate it in Storyboard. </p>
  
  <p>I understand that there are similar questions out there, I have read most of them but none have helped me.</p>
  
  <p>Thank you all for any help.</p>