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

查看:19
本文介绍了如何通过代码从 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.

在它的 -viewDidLoad 方法中添加 viewController 作为通知的监听器:

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 的位置,使用此行:

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天全站免登陆