Ios 从场景中显示UIViewController
我正在尝试从SKScene演示UIViewController,但我不知道如何实现。谷歌搜索提供的大部分内容是如何从一个场景中显示共享屏幕,但这不是我想要做的。只是一个常规的视图控制器,它有一个按钮和一个标签(可能还有一个图像) 这是我的简介中的代码。这不是游戏场景,只是一个带有标题、播放按钮和删除广告按钮的介绍屏幕:Ios 从场景中显示UIViewController,ios,objective-c,iphone,uiviewcontroller,sprite-kit,Ios,Objective C,Iphone,Uiviewcontroller,Sprite Kit,我正在尝试从SKScene演示UIViewController,但我不知道如何实现。谷歌搜索提供的大部分内容是如何从一个场景中显示共享屏幕,但这不是我想要做的。只是一个常规的视图控制器,它有一个按钮和一个标签(可能还有一个图像) 这是我的简介中的代码。这不是游戏场景,只是一个带有标题、播放按钮和删除广告按钮的介绍屏幕: #import "MyScene.h" #import "Intro.h" #import "PurchasedViewController.h" @interface Pur
#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声明'presentViewController:animated:completion'没有可见的@interface”
我有点困惑,因为我以前从未尝试从SKScene中演示UIViewController,也无法在web上找到解决方案。我找到了一个比大多数解决方案简单得多的解决方案:
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];
为什么将您的
SKScene
命名为PurchasedViewController
?:-)SKScene实际上被称为Intro.m。Purchased View Controller是我试图从SKScene中呈现的UIViewController。我编辑了代码以反映这一点。无论如何,您不能从SKScene调用presentViewController:animated:completion:
e
。从UIViewController
调用它。您可以使用NSNotificationCenter
让view controller知道应该显示新的view controller。将SKScene称为view controller进一步混淆了这一问题。Sprite Kit类与标准view controller不同,这正是任务的不同之处ficult适用于没有经验的人。这与标准iOS应用程序中从一个视图控制器到另一个视图控制器的过程不同。请参阅此问题和我的答案: