title: Push a ViewController from a Cocos2d scene
body: <p>I am loading a cocos2d game inside an Storyboard. I implemented a CocosViewController that loads the cocos2d scene (IntroLayer) and IntroLayer that starts the game. I would like to go back to CocosViewController or open a new viewcontroller when the timer (countdown) is zero in the game. </p>
  
  <p>I tried calling <code>[[CCDirector sharedDirector] popScene];</code> when the countdown arrives to zero and with <code>CocosViewController *cocos = [[CocosViewController alloc] init]; [cocos.navigationController dismissViewControllerAnimated:YES completion:nil];</code>
  in onExit method but the app crashes when the countdown arrives to zero, any suggestion? </p>
  
  <p>My CocosViewController set up cocos2d</p>
  
  <pre><code>-(void)setupCocos2d {
     CCDirector *director = [CCDirector sharedDirector];
     //[[[self navigationController] navigationBar] setHidden:YES];
  
  
      if([director isViewLoaded] == NO)
      {
          // Create the OpenGL view that Cocos2D will render to.
          CCGLView *glView = [CCGLView viewWithFrame:self.view.bounds
                                         pixelFormat:kEAGLColorFormatRGB565
                                         depthFormat:0
                                  preserveBackbuffer:NO
                                          sharegroup:nil
                                       multiSampling:NO
                                     numberOfSamples:0];
  
          // Assign the view to the director.
          director.view = glView;
  
  
          // Initialize other director settings.
          [director setAnimationInterval:1.0f/60.0f];
          [director enableRetinaDisplay:YES];
      }
  
  // Set the view controller as the director's delegate, so we can respond to certain events.
  director.delegate = self;
  
  // Add the director as a child view controller of this view controller.
  [self addChildViewController:director];
  
  
  // Add the director's OpenGL view as a subview so we can see it.
  [self.view addSubview:director.view];
  [self.view sendSubviewToBack:director.view];
  
  // Finish up our view controller containment responsibilities.
  [director didMoveToParentViewController:self];
  
  // Run whatever scene we'd like to run here.
  NSArray *parameters = [[NSArray alloc] initWithObjects:@"3", @"sound", @"countdown", nil];
  if(director.runningScene)
      [director replaceScene:[IntroLayer scene];
  else
      [director pushScene:[IntroLayer scene];
  }
  
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      NSLog(@"cocos2d controller viewdidload");
      [self setupCocos2d];
  }
  
  -(void)viewDidUnload {
      NSLog(@"did unload");
      [[CCDirector sharedDirector] setDelegate:nil];
      [[CCDirector sharedDirector] end];
  }
  </code></pre>
  
  <p>IntroLayer creates the scene for the game. This is my onExit method:  </p>
  
  <pre><code> -(void) onExit{
          NSLog(@"Introscene exit");
          [super onExit];
      }
  </code></pre>
  
  <p>And this is the game. I want to load a viewcontroller when the game finished. </p>
  
  <pre><code>-(void)update:(ccTime)dt{
      if (myTime &gt; 0) {
          myTime -= (float)dt;
          [timeLabel setString:[NSString stringWithFormat:@"%.2f", myTime]];
      } else {
          myTime = 0;
          [timeLabel setString:[NSString stringWithFormat:@"0.00"]];
          [[CCDirector sharedDirector] popScene];
      }
  
      if (clicks &lt; currentClicks) {
          clicks = currentClicks;
          //[clicksLabel setString:[NSString stringWithFormat:@"%i", clicks]];
      }
  }
  
  -(void) onExit
  {
      [super onExit];
  
      [[CCDirector sharedDirector] stopAnimation];
       CocosViewController *cocos = [[CocosViewController alloc] init];
      [cocos.navigationController dismissViewControllerAnimated:YES completion:nil];
  
      //  [cocos.navigationController popViewControllerAnimated:YES];
  
      //AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
      //[app.navController popViewControllerAnimated:YES];
      //dismissModalViewControllerAnimated:YES
  
      NSLog(@"game exit");
  }
  </code></pre>