0

I'm trying to have two NSPopupButton that are linked to each other and am having issues with the bindings on the 'child' button.

I have three Core Data entity types: Order, Client, and Station. Station -> Client is a many-to-1, so a Client has multiple Station, but a Station can only point to one client.

An Order has a single Station attached to it, so I created all the bindings like I show below. I can properly pick any client from the first dropdown, but then the stations dropdown just shows the currently selected station. I don't see all the stations available, and if I change the client, I don't see a new list of stations to choose from.

Order Controller

An NSObjectController bound to the order entity property in the view controller.

Clients Array Controller

Just binds to the view controller's managedObjectContext to get all clients.

Stations Array Controller

enter image description here enter image description here

Client NSPopupButton

Content Binding Content Values Binding Selected Index Binding

Station NSPopupButton

Station Dropdown Content Binding Station Dropdown Content Values Binding Station Dropdown Selected Object Binding

Gargoyle
  • 9,590
  • 16
  • 80
  • 145
  • What is the relation between order and client? – Willeke Jun 02 '16 at 23:16
  • Is the content of the order controller an order or a client? – Willeke Jun 03 '16 at 00:13
  • Sorry, the order controller has an order. client was a typo. An order is for a specific station, and that station belongs to a client. Think of it as radio stations. You have multiple stations all owned by a parent company (the client) – Gargoyle Jun 03 '16 at 03:47
  • Does the order have a client property? – Willeke Jun 03 '16 at 13:40
  • No. Order has a station property, which in turn has a client property. Because a station belongs to a client it doesn't have to store client as well. But when a human edits via the UI they have to pick a client first because station names may not be unique, even though they have a unique identifier. – Gargoyle Jun 03 '16 at 14:22

1 Answers1

0

The selected value of the client popup button is used to filter the stations, it shouldn't change the client of the station of the order. Bind Selected index (instead of Selected Object) of the client popup button to the Client Array Controller, Controller Key selectionIndex, no Model Key Path.

Bind Selected Object of the station popup button to the Order Object Controller, Controller Key selection, Model Key Path station.

When the user selects a client, the selected station is still the station of the order. If this station has a different client, the station popup button doesn't automagically select another station. You can fix this in the action of the client popup button. For example:

- (IBAction)clientAction:(id)sender {
    NSArray *stations = self.stationArrayController.arrangedObjects;
    if (![stations containsObject:order.station]) {
        if (stations.count > 0)
            self.order.station = stations[0];
        else
            self.order.station = nil;
    }
}
Willeke
  • 14,578
  • 4
  • 19
  • 47
  • As soon as I select a client the clientAction is called, and at that point the stationArrayController has not yet populated with the new values apparently. – Gargoyle Jun 03 '16 at 18:02
  • Weird, in my test app the popup button first updates the bindings and then calls the action method. – Willeke Jun 05 '16 at 10:11
  • I'm still struggling with this. I updated the question to show my current bindings. When I'm running now I get an exception about NSCFNumber count not recognized. It looks like it's just failing to do the initial bindings now. – Gargoyle Jun 20 '16 at 20:37
  • 1
    Remove the Selected Indexes binding of the Stations Array Controller. You don't need it and it is binding a set to a number. – Willeke Jun 20 '16 at 21:53
  • Thanks! I also changed it to be ContentSet instead as per the error I was getting for the stations array. My only piece left to solve is the initial values when I edit an existing order. I can call `clientDropDown.selectItemWithTitle(order.stations.client.name)` in `viewDidLoad` and that sets properly, but the station array controller doesn't 'trigger' that an update was made, so my list of stations is wrong at that point still. – Gargoyle Jun 20 '16 at 22:28
  • OK, got it all working now. I switched to getting the indexOf the selected object from the array controller and then calling setSelectionIndex() on the array controller instead of setting based on the button. – Gargoyle Jun 20 '16 at 22:38