在ViewContollers之间共享信息
我正在使用Sprite开发游戏,游戏运行得非常好。 我正在尝试添加一个允许您更改GameView上的内容的function。 但该function的按钮位于不同的ViewController上。 如何将代码实现到不同的视图控制器中?
这是我希望按钮实现的代码
for touch: AnyObject in touches { // Get the location of the touch in this scene let location = touch.location(in: self) // Check if the location of the touch is within the button's bounds if button.contains(location) { Ghost.texture = SKTexture(imageNamed:"Ghost2") } }
这是按钮
@IBAction func button(_ sender: AnyObject) {
但是按钮位于ViewController1上,我要更改的对象是ViewController2(GemScene)
看看这个问题:
在视图控制器之间传递数据 (其中一个答案显示了Swift 3语法。)
无论如何,我个人在这些情况下所做的就是使用NotificationCenter ,它甚至在不同的ViewControllers之间提供松散耦合的消息。 你可以看一下: Swift 3中的NSNotifications