簡體   English   中英

展示SKScene的UIViewController

[英]Present a UIViewController from SKScene

我正在嘗試從SKScene提供一個UIViewController,但我不知道該怎么做。 Google搜索提供的大部分功能是如何顯示SKScene的共享屏幕,但這不是我要嘗試的。 只是一個普通的View Controller,它將具有一個按鈕和一個標簽(也可能是圖像)。

這是我的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給我的錯誤是“ Intro沒有可見的@interface聲明'presentViewController:animated:completion'。

我有些困惑,因為我之前從未嘗試過通過SKScene呈現UIViewController,也無法在網絡上找到解決方案。

我找到了一個比大多數解決方案簡單得多的解決方案:

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

如果您在情節提要中創建序列並為其指定標識符,我還想出了一種更簡單的襯板:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM