7

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

4

3 回答 3

9

您不能从 SKScene 中呈现 viewController,因为它实际上只在 SKView 上呈现。您需要一种向 SKView 的 viewController 发送消息的方法,而后者又会显示 viewController。为此,您可以使用委托或 NSNotificationCenter。

代表团

将以下协议定义添加到您的 SKScene 的 .h 文件中:

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

并在接口中声明一个委托属性:

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

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

[self.delegate showDifferentView];

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

@interface ViewController : UIViewController <sceneDelegate>

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

scene.delegate = self;

然后在此处添加以下方法:

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

NSNotificationCenter

保留上一个替代方案中描述的 -showDifferentView 方法。

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

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

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

[[NSNotificationCenter defaultCenter] postNotificationName:@"showDifferentView" object:nil];
于 2014-03-21T10:15:26.697 回答
2

我建议以下选项:(这在你的场景中)

-(void)presentViewController{

    MyViewController *myController = [[MyViewController alloc]init];
//Or instantiate any controller from the storyboard with an indentifier
[self.view.window.rootViewController presentViewController:myController animated: YES options: nil];
}

稍后在您的视图控制器中,当您希望将其关闭时,请执行以下操作:

-(void)dismissItself:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

这个选项的好处是你不需要存储你的视图,因为你可以在任何时候初始化它,只要你所在的场景导入视图控制器的代码。

让我知道它是否有效

于 2014-03-21T13:55:48.057 回答
0

ZeMoon,很好的答案,但是如果多个场景想要显示同一个视图控制器怎么办,例如一些设置屏幕?您不想定义多个执行相同操作的场景委托协议。

另一个想法是定义一个协议来为您处理不同视图控制器的呈现:

@protocol ScreenFlowController <NSObject>
- (void)presentSettingsScreenFromScene:(SKScene *)scene;
- (void)presentCreditsScreenFromScene:(SKScene *)scene;
@end

场景参数可用于根据您来自哪里做出决定。

您的游戏(或任何其他对象)的视图控制器实现了该协议。所有显示不同屏幕的场景都会收到对控制器的弱引用:

@property (nonatomic, weak) id<ScreenFlowController> screenFlowController;
于 2016-02-01T12:15:30.733 回答