【问题标题】:Sharing information between ViewContollers在 ViewContoller 之间共享信息
【发布时间】:2025-12-25 03:05:06
【问题描述】:

我正在使用 Sprite 开发游戏,游戏运行良好。我正在尝试添加一个功能,允许您更改该 GameView 上的内容。但是该功能的按钮位于不同的 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) 上

【问题讨论】:

标签: ios swift3


【解决方案1】:

看看这个问题:

Passing Data between View Controllers(其中一个答案显示了 Swift 3 语法。)

无论如何,我个人在这些情况下所做的就是使用NotificationCenter,即使在不同的 ViewController 之间也能提供松散耦合的消息。你可以看这里:NSNotifications in Swift 3

【讨论】: