Search code examples
objective-ccocoainterface-buildercocoa-bindings

Cocoa bindings: combine fixed items with ArrayController bound items in an NSPopupButton


I'm trying to make a color chooser using an NSPopupButton, and at the bottom i'd like a separator and a "Custom…" option. Is it possible to load the colors from array, then tack the separator and "custom" item on bottom? if so, how?

Thanks!


Solution

  • You should really use a NSColorWell instead of hand-crafting your own. One of the reasons why Apple has superior GUIs than other platforms (especially Linux) is because developers use the standard components for doing this kind of thing. Arguments like 'because I think it makes the interface more concise' are the reasons why GIMP is such a prime example of how not to design a GUI.

    That said, basically what you're trying to do is define a dynamic menu, rather than a fixed-size list (as one might do in InterfaceBuilder). You can do this via the NSMenu and NSMenuItem classes.

    MenuList documentation guide

    What you'd need to do, is rather than display the menu on-demand, is fill it when the application starts up with the default array. Then, when the array changes (via your model objects) trigger the re-creation of the menu. Alternatively, trap the menu with the menuNeedsUpdate: message.