How to present a modal view in sprite kit?

*爱你&永不变心* 提交于 2019-12-05 00:10:07


How can I use Game Center or the GameKit Framework with a Sprite Kit Xcode template? In Sprite kit, it uses Scenes; but normally to view the leaderboards for example you need to "presentModalViewController" but that is not possible in SKView.

Thanks in advance!


Smick pointed me in the right direction for a similar problem I was having. I needed to send a message to the main ViewController from my SKScene. This did it for me:

In your SKScene, import the ViewController

#import "MyViewController.h"

Then send it a message:

[(MyViewController *)self.view.window.rootViewController  myMethod];

You could try creating a method in the ViewController that opens the GameKit view for you, that is triggered from the SKScene. Thanks Smick!


presentModalViewController must be called on the view controller your SKView sits on.


You can also set up the settings view in the storyboard, ie

enter image description here

Then if you add a sprite and use for a button i.e. _settingsBtn it will perform the segue

    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
        UITouch* touch = [touches anyObject];
        CGPoint location = [touch locationInNode:self];

        if ([_settingsBtn containsPoint:location]) {
            UIViewController *vc = self.view.window.rootViewController;
            [vc performSegueWithIdentifier:@"settingsPushSegue" sender:self];

Then you can use a unwind segue to remove it, just a a UIButton.

Have this in your view controller class..

- (IBAction)unwindToHideSettingsModal:(UIStoryboardSegue *)unwindSegue

So now on the storyboard, control drag from your button you added to the green exit segue, and select the above unwind segue.

enter image description here

Now you will have it show and hide as expected. You can design your settings UI ect in the storyboard.

That should get you started.