views:

269

answers:

2

I have a list of applications and I'd like to make an NSPopupButton that shows a menu of application names with their icon to the left of each item.

I've been able to bind the NSPopupButton to my array of items, but there isn't a binding entry for an image. I thought I could put a cell in there and bind the cell as an image and as text, but I can't find an appropriate cell in IB.

Is there a clean and simple way to do this using bindings? Do I have to write a custom cell?

A: 

NSMenuItem has Image binding (in "Parameters" section waaaaaayyyyy down). So I think that you should bind that value to a path in your array of running applications. You can get an icon for your app using shared NSWorkspace object.

Eimantas
That would be great if he were binding a single already-existing menu item. The question implies that he has bound the pop-up button itself, to have it dynamically generate the menu items.
Peter Hosey
+2  A: 

There is no way to bind the images as well as the titles of the menu items using a stock NSPopUpButton. You will have to subclass it and write an IBPlugin to expose the subclass. And, of course, you should handle the cell as well.

I've found mixing Bindings with NSPopUpButton to be a bag of hurt for a variety of reasons, including the impossibility of separators and of out-of-model menu items such as “Default” or “All”. Consider using a different control, such as a source list, or populating and re-populating the pop-up menu manually.

Peter Hosey
I might add that I've found some good sample code that at least gives me the cell...http://developer.apple.com/mac/library/samplecode/SourceView/index.htmlThere is an ImageAndTextCell class inside.
Michael Bishop
Yes, but that won't help you with an NSPopUpButton, which requires an NSPopUpButtonCell. Another kind of cell will not know how to display and track a menu. There isn't a way to appoint a second cell to the job of displaying the content objects, since that's the menu items' job.
Peter Hosey