Cocoa双层弹出绑定

时间:2016-04-08 21:47:25

标签: cocoa-bindings nspopupbutton

我有三个NSObject子类:Order,Station和Client。订单有一个指向Station和Client的指针。在我的订单编辑器表单中,我已经为Station和Client提供了NSPopupButtons,并且这些成功绑定到NSArrayControllers。当客户端更改时,工作站弹出窗口将重置为该新客户端的有效工作站。

我通过KVO在弹出按钮的selectedIndex属性上处理它,然后更改Order对象上的客户端或工作站。

我遇到的问题是,当我第一次加载表单时,会调用这些方法,它们基本上将它们重置为列表中的第一项,从订单中覆盖“实际”客户端和工作站。

能够进行此类观察的正确方法是什么,但仍然在弹出窗口上设置初始值?

举个例子,这是我处理客户端弹出窗口的方式:

dynamic var clientSelectedIndex: NSIndexSet! {
    willSet {
        guard let undoManager = undoManager, oldIndex = clientSelectedIndex?.firstIndex where oldIndex != NSNotFound else { return }

        let station = order.station
        let client = order.client
        let csi = clientSelectedIndex

        undoManager.registerUndoWithTarget(self) {
            target in
            target.order.client = client
            target.order.station = station
            target.clientSelectedIndex = csi
        }
    }
    didSet {
        guard let index = clientSelectedIndex?.firstIndex where index != NSNotFound else { return }

        order.client = clients[index]
        order.station = order.client.stations.first!
    }
}

因此,当表单加载时,在调用viewDidLoad之前,该索引将设置为0,这将覆盖order.client中第一个可能的客户端选项。

0 个答案:

没有答案
相关问题