【问题标题】:How do I go from SKScene to UIViewController by code?如何通过代码从 SKScene 转到 UIViewController?
【发布时间】:2014-04-28 06:13:28
【问题描述】:

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

【问题讨论】:

  • 通用问题,通用答案:场景是具有关联视图控制器的 SKView 的一部分。您只能 segue 视图控制器。因此,您可以像任何其他 UIViewController 一样从和到 SKView 的视图控制器。

标签: ios uiviewcontroller sprite-kit segue skscene


【解决方案1】:

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

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

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

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

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

@property (nonatomic, weak) id<ScreenFlowController> screenFlowController;

【讨论】:

    【解决方案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];
    }
    

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

    让我知道它是否有效

    【讨论】:

      【解决方案3】:

      您不能在 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 方法。

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

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

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

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

      【讨论】:

      • NSNotificationCenter 的东西运行良好。非常感谢。我还将 NSNotificationCenter 用于我的应用程序的其他部分。
      • 不客气。最后一句警告:不要过度。通知非常有用,但在某些情况下,您可能想在全球范围内爆出数据,而实际上您只有一个真正对数据感兴趣的委托。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 2015-01-19
      相关资源
      最近更新 更多