Changing the visibility of NSPopUpButton items based on keystrokes

I have an application here that needs to be read into a bunch of data from an external file and displayed as NSPopUpButton in the Cocoa user interface. The catch here is that the data that is being read must have a flag that indicates whether it is considered β€œhidden” or not.

If the data is hidden, it must be added to NSPopUpButton as NSMenuItem , but the hidden flag should be set to YES , so it usually does not appear in the NSPopUpButton menu. If the user holds the magic key on his keyboard (usually in this case ALT), then hidden objects must be hidden. If the user allows you to switch from the ALT key, then they should be automatically hidden, except for the one that could be selected - which will become hidden if another NSMenuItem is selected.

Actually, I have a hell of a time to figure this out.

I was wondering if there is a direct way to do this using NSArrayController and NSPopUpButton , but so far I have not been able to find anything like a solution - not when it comes to managing the hidden property of NSMenuItem objects.

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

+4
source share
1 answer

You can connect the popup to the array controller and change the filter predicate. From an MVC design point of view, you will not use an attribute of type "hidden", which is a characteristic of the view, but possibly "extended." Normally set the filter predicate on the array controller to "advanced = no". Then, when the user holds your preferred modifier, remove the predicate. The popup will be updated automatically. The array controller must be bound to the property of the array on another object (in your data model). The popup should be bound to allocObjects on the array controller.

+1
source

Source: https://habr.com/ru/post/1383548/


All Articles