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:
This nicely populates pop up with titles of all categories. I can also bind Selected Object to:
Blog
object)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 :)
I would try it as follows:
Of NSPopupButton:
arrangedObjects
arrangedObjects
Model Key Path: title
(empty)
Model Key Path: content.category
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With