0

How to pop to rootViewController from SKScene?

im trying to build a game with menu screen. There are two scenes - one for menu and one for game.

I have problem with jumping to rootView from a game scene. Game scene has SKScene with menu button - SKLabelNode. When touched it should move player to menu screen.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    SKNode *touched = [self nodeAtPoint:[touch locationInNode:self]];
    if ([touched.name isEqualToString:BUTTON_MENU_NAME]) {

        UIViewController *vc = self.view.window.rootViewController;
        [vc.navigationController popToRootViewControllerAnimated:YES];
    }
}

Touch is detected correctly but nothing happens.

Maq
  • 369
  • 3
  • 13

1 Answers1

2

Probably the initial view controller is the navigation controller so I suggest doing:

UINavigationController *vc = (UINavigationController *)appdelegate.window.rootViewController;
[vc popToRootViewControllerAnimated:YES];
chriskryn
  • 121
  • 1
  • 4
  • After hours of pulling my hair out your answer is awesome! I had to change it to this. UINavigationController *vc = (UINavigationController *)self.view.window.rootViewController; [vc popToRootViewControllerAnimated:YES]; – Jeff Mar 06 '16 at 01:06