本文介绍了展示来自SKScene的UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过SKScene呈现UIViewController,但我不知道该怎么做.Google搜索提供的大部分功能是如何显示SKScene的共享屏幕,但这不是我要尝试的方法.只是一个普通的View Controller,它将具有一个按钮和一个标签(也可能是图像).

I'm trying to present a UIViewController from a SKScene and I can't figure out how to do it. Most of what a Google search provides is how to present a share screen from a SKScene, but that is not what I'm trying to do. Just a regular View Controller that will have a button and a label (maybe a image as well).

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

Here's my code in my Intro.m. This is not the Gameplay scene, just a intro screen with a title, play button and a remove ads button:

#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给我的错误是"Intro没有可见的@interface声明'presentViewController:animated:completion'.

The error Xcode gives me is "No visible @interface for Intro declares 'presentViewController: animated: completion'.

有点困惑,因为我之前从未尝试过通过SKScene呈现UIViewController,也无法在网络上找到解决方案.

I'm a bit stumped because I haven't tried to present a UIViewController from a SKScene before and can't find a solution on the web.

推荐答案

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

I found a solution that is much simpler than most of the solutions out there:

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

如果您在情节提要中创建序列并为其指定标识符,我还想出了一种更简单的衬板:

I also figured out an even simpler one liner if you create a segue in Storyboard and give it an identifier:

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

这篇关于展示来自SKScene的UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 20:41