0

I have tried many things, none of which are working. I have an SKScene, and when the game is over, I want it to go back to the original view controller. Thank you in advance.

I have tried this in my SKScene

homeScreenViewController *viewCont = [[homeScreenViewController alloc] init];
            [viewCont viewDidLoad];

This in my other view controller

constructinoViewController *view = [[constructinoViewController alloc ] init];
        [self presentViewController:view animated:YES completion:nil];

It mostly says The view is not in the view hierarchy.

CSchulz
  • 10,882
  • 11
  • 60
  • 114
user2918201
  • 52
  • 1
  • 7

2 Answers2

0

You need a way to send the parent viewController a message.

In your viewController which presents the SKScene, add the following line in viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeScene) name:@"closeScene" object:Nil];

Also, add this method

-(void)closeScene
{
    //Remove the SKView, or present another viewController here.

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"closeScene" object:nil];
}

Then in your SKScene, at the point where your game ends,

[[NSNotificationCenter defaultCenter] postNotificationName:@"closeScene" object:nil];

You can do this by using delegates as well.

ZeMoon
  • 20,054
  • 5
  • 57
  • 98
  • Thank you so much!! The only problem is that it still does not go back to the view controller. The closeScene method IS being called. Do i need to do something else in addition? – user2918201 Feb 27 '14 at 16:42
  • Ok, please attach two more parts of code to the question. 1- which is used to present the skscene. 2- the class names of the viewController in which you are presenting the SKScene and the one which you want to present from the scene – ZeMoon Feb 27 '14 at 16:53
  • Ok, The scene is presented from "levelOneViewController", and it is presented like SKView * skView = (SKView *)self.view; skView.showsFPS = NO; skView.showsNodeCount = NO; // Create and configure the scene. SKScene * scene = [level1 sceneWithSize:skView.bounds.size]; scene.scaleMode = SKSceneScaleModeAspectFill; // Present the scene. [skView presentScene:scene]; – user2918201 Feb 27 '14 at 18:10
  • The classs i want to presnet from the skscene is homeViewController – user2918201 Feb 27 '14 at 18:10
  • You need to put in more code in the closeScene method which will actually present the new viewController. I left that part with a comment so you could post some code in there as well. – ZeMoon Feb 28 '14 at 05:21
  • Also, you are initializing the viewController incorrectly if you are using an XIB. it should be: homeScreenViewController *viewCont = [[homeScreenViewController alloc] initWithNibName:@"homeScreenViewController" bundle:nil]; [viewCont viewDidLoad]; – ZeMoon Feb 28 '14 at 05:22
  • It is with a story board – user2918201 Feb 28 '14 at 13:42
0

If you have presented the ViewController, then you need to dismiss it to return to previous ViewController.

You should use

[self dismissViewControllerAnimated:YES completion:NULL];
Srikanth
  • 1,861
  • 17
  • 29
  • Unfortunately, where would I put this? I cannot put it in an SKScene – user2918201 Feb 28 '14 at 13:41
  • You need to send a notification to your view controller which added your SKScene to its view. Consider ViewController1 presented ViewController2 and in ViewController2's view, you added your game scene. Once the game is over, fire a notification to ViewController2 and in the notification function, write the above code. – Srikanth Feb 28 '14 at 17:03