【问题标题】:Present a UIViewController from SKScene从 SKScene 呈现一个 UIViewController
【发布时间】:2014-09-06 03:11:57
【问题描述】:

我正在尝试从 SKScene 呈现 UIViewController,但我不知道该怎么做。谷歌搜索提供的大部分内容是如何从 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 给我的错误是“No visible @interface for Intro declares 'presentViewController: animated: completion'。

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

【问题讨论】:

  • 你为什么把你的SKScene命名为PurchasedViewController? :-)
  • SKScene 实际上被称为 Intro.m。购买的视图控制器是我试图从 SKScene 展示的 UIViewController。我编辑了代码以反映这一点。
  • 无论如何,您不能从SKScene 调用presentViewController:animated:completion:。从UIViewController 拨打电话。您可以使用NSNotificationCenter 让视图控制器知道应该呈现新的视图控制器。
  • 将 SKScene 称为视图控制器会进一步混淆问题。 Sprite Kit 类与标准 View Controller 不同,这使得没有经验的人难以完成任务。这与标准 iOS 应用程序中从一个 View Controller 到另一个 View Controller 的过程不同。跨度>
  • 查看这个问题和我的回答:stackoverflow.com/questions/21578391/…

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


【解决方案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];

【讨论】:

    猜你喜欢
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    相关资源
    最近更新 更多