在 ViewContoller 之间共享信息 [英] Sharing information between ViewContollers

查看:22
本文介绍了在 ViewContoller 之间共享信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Sprite 开发游戏,游戏运行良好.我正在尝试添加一项功能,允许您更改该 GameView 上的内容.但是该功能的按钮位于不同的 ViewController 上.我如何才能将代码实现到不同的视图控制器中?

I am developing a game using Sprite and the game works perfectly fine. I am trying to add in a feature that allows you to change things on that GameView. But the buttons for the feature are on a different ViewController. How would I be able to have the code be implemented into a different view controller?

这是我希望按钮实现的代码

This is the code i want the button to implement

        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) 上

} But the button is on ViewController1 and the object that i want to change is on ViewController2 (GemScene)

推荐答案

看看这个问题:

在视图控制器之间传递数据(其中一个答案显示了 Swift 3 语法.)

Passing Data between View Controllers (One of the answers shows Swift 3 syntax.)

无论如何,我个人在这些情况下所做的是使用NotificationCenter,它甚至在不同的 ViewControllers 之间也提供松散耦合的消息.您可以在这里查看:Swift 3 中的 NSNotifications

Anyway, what I personally have done in these cases is to use NotificationCenter, which provides loosely coupled messages even between different ViewControllers. You can take a look here: NSNotifications in Swift 3

这篇关于在 ViewContoller 之间共享信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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