如何从 Sprite Kit 场景中引用当前的 Viewcontroller [英] How to reference the current Viewcontroller from a Sprite Kit Scene

查看:29
本文介绍了如何从 Sprite Kit 场景中引用当前的 Viewcontroller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到这个问题的答案,我认为这并不难.

I'm having a hard time finding the answer to this question I assume is not that hard.

如何从 SKScene 引用视图控制器上定义的方法和属性?

How can I reference methods and properties defined on a viewcontroller from a SKScene ?

并以此为基础:如何从另一个 SKScene 中加载的 SKScene 引用 ViewController?

And building on that: How can you reference the ViewController from a SKScene that was loaded from within another SKScene?

推荐答案

比我的反向引用"答案更好的选择是使用协议显式定义 SKScene 子类期望的方法UIViewController 子类来实现.以下是您需要完成的步骤.

A better option than my "back reference" answer is to use a protocol to explicitly define the methods that a SKScene subclass expects the UIViewController subclass to implement. Here are the steps you'll need to go through.

1) 定义 UIViewController 需要采用并遵守的协议(契约):

1) Define the protocol (contract) that the UIViewController will need to adopt and conform to:

protocol GameManager{
    func loadHomeScene()
    func loadGameScene(level:Int)
    func loadHighScoreScene()
}

2) UIViewController 子类现在采用协议:

2) The UIViewController subclass now adopts the protocol:

类 GameViewController: UIViewController,GameManager{

3) GameScene 获得一个引用 UIViewController 的 ivar:

3) The GameScene gets an ivar that references the UIViewController:

var gameManager:GameManager?

4)当GameManager需要调用UIViewController时,是这样的:

4) When the GameManager needs to call the UIViewController, it does so like this:

gameManager.loadHighScoreScene()

我们现在有了一个明确定义的GameManager 合约.在这个项目上工作的任何其他开发人员(以及当前开发人员未来的自己)现在都知道 GameManager 应该实现哪些方法.这意味着我们可以轻松地将这段代码移植到不同的项目(因此也可以移植到不同的 UIViewController),只要这个新的 UIViewController 采用 GameManager,它仍然可以工作 协议.

We now have a well-defined contract of what a GameManager is. Any other developers (and the current developer's future self) that work on this project now know which methods a GameManager should implement. This means we could easily port this code to a different project (and thus a different UIViewController) and it would still work as long as this new UIViewController adopted the GameManager protocol.

同样,如果我们决定将场景导航代码移出视图控制器,而是将其放在单例类中,则该单例类只需要采用 GameManager 协议即可.

Similarly, if we decided to move our scene navigation code out of the view controller, and instead placed it in a singleton class, this singleton class would merely need to adopt the GameManager protocol.

这个帖子提供了 4 种解决问题的方法,我个人使用了所有 4 种方法.以下是我对它们的看法:

This thread gives 4 approaches to solving the problem, and I have personally used all 4. Here's my take on them:

1) 硬编码对 UIViewController 子类的调用:

1) Hard-coding a call to the UIViewController subclass:

(self.view!.window!.rootViewController as!GameViewController).loadGameScene(1)

优势:1行代码!

缺点:两个类的耦合非常紧密,没有明确的契约.

Disadvantage: A very tight coupling of 2 classes, without an explicit contract.

2) 使用返回 UIViewController 的指针:

2) Using a pointer back to the UIViewController:

优点:只需要很少的代码,不需要协议.

Advantages: Very little code required, No protocol required.

缺点:2个类的紧密耦合.没有明确的合同.

Disadvantages: Tight coupling of 2 classes. No explicit contract.

3) 使用NSNotificationCenter:

优势:松散耦合的类.

缺点:设置广播器、接收器和回调方法需要更多代码.更难跟踪和调试代码.没有明确的合同.通知的一对多"功能很好,但在这里对我们没有帮助.

Disadvantages: more code needed to set up the broadcaster, receiver, and callback methods. Harder to trace and debug the code. No explicit contract. The "one-to many" capability of notifications is nice, but doesn't help us here.

4) 创建协议:

优势:明确的合同.MVC 的众神会很高兴的.

Advantages: An explicit contract. The gods of MVC will be pleased.

缺点:要编写更多代码(但比通知少得多).

Disadvantage: More code to write (but much less than notifications).

这篇关于如何从 Sprite Kit 场景中引用当前的 Viewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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