介绍一下SKScene的UIViewController

时间:2014-09-06 03:11:57

标签: ios objective-c iphone uiviewcontroller sprite-kit

我正在尝试从SKScene呈现一个UIViewController,我无法弄清楚如何做到这一点。 Google搜索提供的大多数内容是如何从SKScene呈现共享屏幕,但这不是我想要做的。只是一个常规的视图控制器,它有一个按钮和一个标签(也许是一个图像)。

这是我的Intro.m中的代码。这不是游戏场景,只是带有标题,播放按钮和删除广告按钮的介绍屏幕:

#import "MyScene.h"
#import "Intro.h"
#import "PurchasedViewController.h"

@interface PurchasedViewController ()

-(id)initWithSize:(CGSize)size {

   if (self = [super initWithSize:size]) {

  .....

   SKSpriteNode *removeAds = [SKSpriteNode spriteNodeWithImageNamed:@"removeAds"];
     removeAds.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame)/6);
     removeAds.zPosition = 1;
     removeAds.name = @"RemoveAds";
    [self addChild:removeAds];
  }

 - (void)performTouches:(NSSet *)touches {    

  for (UITouch *touch in touches) {
    CGPoint pointInSKScene = [self.view convertPoint:[touch locationInView:self.view] toScene:self];
    SKNode *touchedNode = [self nodeAtPoint:pointInSKScene];

    if ([touchedNode.name isEqualToString:@"RemoveAds"]) {
        [self runAction:[SKAction playSoundFileNamed:@"enter.mp3" waitForCompletion:NO]];

        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"PurchasedViewController"];
        [self presentViewController:vc animated:YES completion:nil];
      }
    }
  }

Xcode给我的错误是“没有可见的@interface forIntro声明'presentViewController:animated:completion'。

我有点难过,因为我之前没有尝试过从SKScene呈现UIViewController,也无法在网上找到解决方案。

1 个答案:

答案 0 :(得分:1)

我找到了比大多数解决方案简单得多的解决方案:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"PurchasedVC"];
//Call on the RootViewController to present the New View Controller
[self.view.window.rootViewController presentViewController:vc animated:YES completion:nil];

如果你在Storyboard中创建一个segue并给它一个标识符,我还想出了一个更简单的单行:

[self.view.window.rootViewController performSegueWithIdentifier:@"PurchasedViewController" sender:self];