How to setup bindings for NSPopUpButton

cocoa, cocoa-bindings, nspopupbutton, selection

Solution

I would try it as follows:

Of NSPopupButton:

- Bind Content to : categoriesArrayController ControllerKey: `arrangedObjects`

- Bind Content Values to : categoriesArrayController ControllerKey: `arrangedObjects` Model Key Path: title

- Bind Selected Object to : blogObjectController ControllerKey: `(empty)` Model Key Path: content.category

Problem

I want to bind selection of NSPopUpButton to one of the predefined values. To make it simpler, imagine a blog app: I'd have `BlogPost` and `Category` entities (Core Data, although it doesn't matter) and each `BlogPost` object has a link to one of the `Category` objects (through `category` property). I want to have the user change the category through NSPopUpButton, so in my XIB, I have NSArrayController that holds all possible categories and I bind button's Content Values to: - Bind to: categoriesArrayController - Controller key: arrangedObjects - Model key: title This nicely populates pop up with titles of all categories. I can also bind Selected Object to: - Bind to: blogObjectController (or directly to `Blog` object) - Controller key: selection - Model key: category This works and correct category is selected in pop up, however changing selection doesn't change the category under which the blog post is filed, but instead changes the title of post category to the selected value. Given the above bindings this actually makes sense, so my next step was to change the binding of pop up's content values to only arrangedObjects (no model key), similarly selected object. This works and changing selection does indeed change `category` of the blog post to another one. BUT it doesn't show category title in pop up menu, but instead shows the `description` of the category (which in Core data ends with something like `<Category 0x1002b6990> (entity: Category; id: .......)`. Which also makes sense, so I introduced custom `NSValueTransformer` (non-reversible). This fixes item descriptions in pop up menu, but then selection doesn't work. I also tried using the transformer on selected object binding, but that just disables my pop up entirely (perhaps reverse transform would be required, but this would substantially complicate transformer). Am I missing something obvious - is this possible to achive entirely with bindings, without introducing additional code on controller layer? Any though is welcome! PS: hope above text makes sense :)

Original source