简体   繁体   中英

Presenting a View Controller with an xib from an SKScene

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 .

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.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM