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.