0

I have three NSObject subclasses: Order, Station, and Client. The Order has a pointer to Station and Client. On my order editor form I've got NSPopupButtons for both Station and Client, and those are bound successfully to NSArrayControllers. When the Client changes, the Station popup resets to the valid stations for that new client.

I'm handling that via KVO on the selectedIndex properties of the popup buttons, and that then changes the client or station on the Order object.

The issue I'm having is that when I first load the form those methods are called, which basically resets them both to the first item in the list, overwriting the "actual" client and station from the order.

What's the right way to be able to do that type of observing, but still set the initial value on the popups?

As an example, here's how I'm handling the Client popup changing:

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!
    }
}

So when the form loads, and before viewDidLoad is called, that index gets set to 0, which overwrites what was in order.client with the first possible client choice.

Gargoyle
  • 9,590
  • 16
  • 80
  • 145
  • Do you need the arraycontrollers? Do you want to do everything with bindings? Where do the clients and valid stations come from? – Willeke Apr 09 '16 at 13:04
  • Well to do it via bindings I thought I needed them. The clients and valid stations are in an array in another object. – Gargoyle Apr 12 '16 at 17:32
  • Isn't it easier to use the action of the popup buttons instead of observing the selected indexes of the arraycontroller? – Willeke Apr 14 '16 at 12:33

0 Answers0