4

I've got an application here that needs to read in a bunch of data from an external file and display it as a NSPopUpButton in a Cocoa user interface. The catch here is that the data that is being read in needs to have a flag that states if it is considered "hidden" or not.

If the data is hidden, it needs to be added to the NSPopUpButton as an NSMenuItem, but the hidden flag needs to be set to YES so it does not normally appear in the NSPopUpButton menu. If the user holds down a "magic key" on their keyboard (usually ALT, in this case) then those hidden objects need to be unhidden. If the user lets go of the ALT key, then they need to be automatically re-hidden, except for the one that may have been selected -- which would become hidden if another NSMenuItem were chosen.

I'm kind of having a heck of a time figuring this out, actually.

I was wondering if there is a straight forward way of doing this using NSArrayController and an NSPopUpButton, but thus far I have not been able to find anything resembling a solution -- not when it comes to managing the hidden property of the NSMenuItem objects.

Does anyone know how this can be achieved using Cocoa Bindings?

jscs
  • 63,694
  • 13
  • 151
  • 195

1 Answers1

1

You can wire the popup to an array controller and alter the filter predicate. From an MVC design standpoint, you wouldn't use an attribute like "hidden", which is a view characteristic, but maybe "advanced". Normally, set a filter predicate on your array controller to "advanced = no". Then when the user holds your preferred modifier, remove the predicate. The popup will update automatically. The array controller should be bound to an array property on another object (in your data model). The popup should be bound to arrangedObjects on the array controller.

Bored Astronaut
  • 994
  • 5
  • 10
  • Then how would I go about setting things up so that if a previously-hidden NSMenuItem were selected, it would not re-hide itself until some other NSMenuItem was selected instead? –  Nov 29 '11 at 03:45
  • There's a few ways. You can demote the underlying model object by making it no longer advanced, or you can extend the filter predicate to add an exception for that one object. Although perhaps a more friendly behaviour would be to refrain from removing any of the advanced options while the pop-up is still active. Only reset the filter predicate when the pop-up closes. Just a suggestion. – Bored Astronaut Nov 29 '11 at 22:24
  • Yes, that is the intended behaviour and I've already coded that up. What I'm trying to figure out is if I can setup the predicate to somehow match the current object it is iterating over to an object pointer, and if they are equal- ignore the "advanced" field all together and let the object remain visible. This way the currently selected item of the NSPopUpButton is always visible regardless of the "advanced" property it has set (either YES or NO). –  Nov 30 '11 at 03:51