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

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

问题描述

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

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

如何引用viewcontroller上定义的方法和属性一个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 subclass需要实现 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:

class GameViewController:UIViewController,GameManager {

3) GameScene 获取一个引用 UIViewController的ivar

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.

缺点:两个类的紧耦合。没有明确的合同。

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

3)使用 NSNotificationCenter

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