7

This question is an extension to link
(The question in the link mainly targets, binding NSPopupbutton to a NSArrayController)

I have a Person class having properties NSString *name and NSImage *avatar
I have to show all the names of persons in Popup button as seen in the below image. enter image description here

But now, as requirement has changed, I need to show avatar of person also.
How do I use Cocoa bindings to bind person's avatar to NSPopup button so that it looks like the one in above image for michael(last menu option)


Note: Michael has been temporarily added for demonstration using following code:

person.title = @"Michael";
person.image = [NSImage imageNamed:@"avatar.png"];
[_popupButton.menu addItem:person];
Community
  • 1
  • 1
Kaunteya
  • 3,107
  • 1
  • 35
  • 66
  • NSMenuItem exposes an image binding. Is the question about how to set things up in Interface Builder? Because given access to that NSMenuItem* and an NSImage*, you just create a binding the way you create any other binding. – stevesliva Dec 17 '14 at 18:56
  • How do you dynamically create the NSMenuItems. Because thats where your image is going to be injected. – Warren Burton Dec 22 '14 at 08:23

1 Answers1

0

There are two ways you can achieve that:-

First take the cell-based tableview inside that two column one for image cell and second for text cell. Populate the table through bindings and then add your tableview inside your popup button in the below way:-

NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@""];
NSMenuItem *Item = [[NSMenuItem alloc] initWithTitle:@"NameList" action:NULL keyEquivalent:@""];
[Item setView:self.tableVw];
[theMenu addItem:Item];
[self.popUptn setMenu:theMenu];

Second follow the below simple steps:-

Assuming your array contains the name elements.

1) Select your arrayContoller bind to FileOwner's and ModelKeyPath->array.

2) Select PopUpButton inside binding Inspector->Selected Object bind to FileOwner's and ModelKeyPath->yourString. This will select the required name accordingly.

3) Select PopUpButton inside binding Inspector->Content Valuesand ModelKeyPath->array

4) Now for setting the image inside popupButton refer below code:-

NSMenuItem  *menuItm=[self.popUpBtn itemWithTitle:@"Michael"];
[menuItm setImage:[NSImage imageNamed:@"dot.gif"]];

Edit:-

1) In the first method your text and image both are populating through binding just you need to add that tableview inside your popupbutton.

2) In the second method your popupbutton will display the image for last name but programmatically. And also, if required to display the images for all names then use for loop to set images inside menu item.

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • 1
    Please read the question again. I want to do in Cocoa Bindings only. I was aware of the solution given by you. Also the comment in bounty states "I am a expecting a binding only solution." – Kaunteya Dec 27 '14 at 05:51
  • You can use tableview and through binding you can populate the data. But you need to add that tableview inside your popupbutton. Refer the updated answer. – Hussain Shabbir Dec 27 '14 at 07:35