如何通过代码从SKScene转到UIViewController? [英] How do I go from SKScene to UIViewController by code?

查看:345
本文介绍了如何通过代码从SKScene转到UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的只是当用户触摸skscene中的skspritenode时,它将转到不同的视图,如 performseguewithidentifier 。谢谢你的帮助。我可以发布代码,但它似乎是一个通用的问题所以我认为你不需要任何代码。顺便说一下,我已经想出了如何检测轻敲skspritenode。我一直在看这个很长一段时间,我很难过。请帮忙。

All I want if for when the user touches a skspritenode in the skscene, it will go to a different view like performseguewithidentifier. Thanks for any help. I can post code, but it seems like a generic question so I figured that you wouldn't need any. By the way, I already figured out how to detect the tapping the skspritenode. I have been looking at this for a long time and I am stumped. Please help.

推荐答案

您无法在SKScene中呈现viewController,因为它实际上只在SKView上呈现。您需要一种方法将消息发送到SKView的viewController,后者将呈现viewController。为此,您可以使用委托或NSNotificationCenter。

You cannot present a viewController from within a SKScene as it is actually only being rendered on a SKView. You need a way to send a message to the SKView's viewController, which in turn will present the viewController. For this, you can use delegation or NSNotificationCenter.

委派

添加遵循协议定义到您的SKScene的.h文件:

Add the following protocol definition to your SKScene's .h file:

@protocol sceneDelegate <NSObject>
-(void)showDifferentView;
@end

并在界面中声明一个委托属性:

And declare a delegate property in the interface:

@property (weak, nonatomic) id <sceneDelegate> delegate;

然后,在您想要显示共享屏幕的位置,请使用以下行:

Then, at the point where you want to present the share screen, use this line:

[self.delegate showDifferentView];

现在,在你的viewController的.h文件中,实现协议:

Now, in your viewController's .h file, implement the protocol:

@interface ViewController : UIViewController <sceneDelegate>

并且,在您的.m文件中,在呈现场景之前添加以下行:

And, in your .m file, add the following line before you present the scene:

scene.delegate = self;

然后在那里添加以下方法:

Then add the following method there:

-(void)showDifferentView
{
    [self performSegueWithIdentifier:@"whateverIdentifier"];
}

NSNotificationCenter

保持-showDifferentView方法,如上一个替代方案中所述。

Keep the -showDifferentView method as described in the previous alternative.

将viewController添加为通知的监听器,其中-viewDidLoad方法:

Add the viewController as a listener to the notification in it's -viewDidLoad method:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showDifferentView) name:@"showDifferenView" object:nil];

然后,在要显示此viewController的场景中,使用以下行: / p>

Then, in the scene at the point where you want to show this viewController, use this line:

[[NSNotificationCenter defaultCenter] postNotificationName:@"showDifferentView" object:nil];

这篇关于如何通过代码从SKScene转到UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆