【发布时间】:2017-03-13 18:44:17
【问题描述】:
首先,我不会在视图控制器之间传递数据。我需要将数据从视图控制器传递到另一个类。
情况是这样的:
我有一个视图控制器,它有滑块,然后我有一个相机类。我在 Scene 类中定义了一个相机,一旦我改变了滑块,就需要改变 Scene 类中相机的值。
VC:
class ViewController: {
@IBAction func moveSlider(_ sender: NSSlider) {
// here I want to pass the sender.value to the Camera class
}
}
相机类:
class Camera {
var cameraLocationX ; // I want this value updated once the the slider moved, however the camera instance is defined in the Scene
}
然而,这个Camera 类并没有在VC 中定义。所以我不能使用静态var方法...
class Scene {
let camera = Camera()
..
camera.cameraLocationX; // here it needs to be updated.
}
如何做到这一点?我在 Google 上搜索似乎应该使用 delegate 或 notification ,但是有人可以给我更多说明吗?
【问题讨论】:
-
cameraLocationX 变量是静态变量吗?如果是这样,你可以直接改变它的值对吗?
-
哼,我忘了静态,我可以用静态试试。
-
我想到了观察者模式。
-
@BobMac,你能详细说明一下吗?
-
这似乎是一个不错的起点。 codeproject.com/Articles/328361/…