以编程方式绑定NSArrayController以接收所选对象

时间:2016-09-29 15:14:30

标签: swift cocoa-bindings nsarraycontroller

这是我的代码:

{"key1":"xyz"}

我正在尝试捕捉所选对象的名称,类似于NSTextField(我在IB中做的并且非常简单)。但是我在这里进入控制台:

@IBOutlet var dataArrayController: NSArrayController!

override func viewDidLoad() {
    super.viewDidLoad()
    dataArrayController.addObserver(self,
                                    forKeyPath: "selection.name",
                                    options: .new, context: &myContext)
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    if (context == &myContext) {
        if (keyPath == "selection.name")
        {

            if let newValue = change?[.newKey] {
                print("Selection changed: \(newValue)")
            }
        } else {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }

    }
}

所以我收到了消息,但没有得到该对象。怎么了?

由于 罗伯特

0 个答案:

没有答案