How do I present UIViewController from SKscene wit

2019-03-05 23:55发布

问题:

I am making a game like Flappy Bird. How do I present a UIViewController from SKScene? First of all, I tell my environments

  • Mac OS X 10.9
  • Xcode 5.0.2
  • Sprite Kit(framework), social.framework(framework) are added in my project

My goal is to display a "Share" button upon Game Over. Tapping the share button image should present a SLComposeViewController (Twitter Share). The contents of the scene should not change.
I'd like to solve bellow issue and change display from GameOverScene to tweetSheet(display) composed with social.framework.

The issue

[self presentViewController:tweetSheet animated:YES completion:nil];
//Error:No visible @interface for 'GameOverScene' declares the selector "presentViewController":animated:completion:


My coding files are below(I extracted parts of important codes).

ViewController.h

import <UIKit/UIKit.h>
import <SpriteKit/SpriteKit.h>
import <iAd/iAd.h>

@interface ViewController : UIViewController<ADBannerViewDelegate><br>

@end

GameOverScene.h

#import <SpriteKit/SpriteKit.h> 

@class SpriteViewController;

@interface GameOverScene : SKScene {
}
@end

GameOverScene.m

#import "GameOverScene.h"
#import "NewGameScene.h"
#import "MainScene.h"
#import <Social/Social.h>
@implementation GameOverScene {
   //The twitter button 
    SKSpriteNode *_twitterbutton;
}
- (id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size]) {
        //Creating the twitterbutton with the twitterbutton image from Images.xcassets
        _twitterbutton = [SKSpriteNode spriteNodeWithImageNamed:@"twitterbutton"];
        [_twitterbutton setSize:CGSizeMake(50, 50)];
        [_twitterbutton setPosition:CGPointMake(self.size.width/2, self.size.height/5 + 50)];
        //Adding the twitter button
        [self addChild:_twitterbutton];
        //Again, this is important, otherwise we can't identify what button is pressed
        _twitterbutton.name = @"twitterbutton";
        [_twitterbutton setPosition:CGPointMake(self.size.width/2, self.size.height/5 + 50)]
    }
    return self;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Same as in NewGameScene menu
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    //Is the twitter button touched?
    if([node.name isEqualToString:@"twitterbutton"]){
        if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
            SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
            [tweetSheet setInitialText:@"TestTweet from the Game !!"];
            [self presentViewController:tweetSheet animated:YES completion:nil];
     **//Error:No visible @interface for 'GameOverScene' declares the selector "presentViewController":animated:completion:**
         } 
    }

ViewControlloer.m

#import "ViewController.h"
#import "NewGameScene.h"
@implementation ViewController
//Loads the view onto our main class
- (void)loadView
{
  self.view  = [[SKView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
}
//Executes when view finishes loading
- (void)viewWillLayoutSubviews
{
  [super viewDidLoad];

  //Set the resize mode to flexible width and height
  [self.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

  //Create our view from our original view
  //Make sure to leave originalContentView in, otherwise the app will crash
  SKView *skView = (SKView *)self.originalContentView;

  //We create a new NewGameScene according to the current dimensions
  SKScene *scene = [NewGameScene sceneWithSize:skView.bounds.size];

  //Create a transition class with animation type fade and a duration of .4 seconds
  SKTransition *transition = [SKTransition fadeWithDuration:.4];

  //Present the menu view (NewGameScene) with our fade in transition
  [skView presentScene:scene transition:transition];
}

@end

回答1:

You cannot present a viewController from within a SKScene as it is actually only being rendered on a SKView. You need a way to send a message to the viewController, which in turn will present the viewController. For this, you can use delegation.

Add the following protocol definition to your SKScene's .h file:

@protocol sceneDelegate <NSObject>
-(void)showShareScreen;
@end

And declare a delegate property in the interface:

@property (weak, nonatomic) id <sceneDelegate> delegate;

Then, at the point where you want to present the share screen, instead of the line:

[self presentViewController:tweetSheet animated:YES completion:nil];

Use this line:

[self.delegate showShareScreen];

Now, in your viewController's .h file, implement the protocol:

@interface ViewController : UIViewController <sceneDelegate>

And, in your .m file, add the following line before you present the scene:

scene.delegate = self;

Then add the following method there:

-(void)presentShareScreen
{

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
        composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:@"TestTweet from the Game !!"];
        [self presentViewController:tweetSheet animated:YES completion:nil];

    }
}

An alternate method would be to use NSNotificationCenter

Keep the -presentShareScreen method as described in the previous alternative.

Add the viewController as a listener to the notification in it's -viewDidLoad method:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentShareScreen) name:@"showShareScreen" object:nil];

Then, in the scene at the point where you want to show this viewController, use this line:

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