Reputation: 3090
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.
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];
Upvotes: 7
Views: 1104
Reputation: 15025
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 Values
and 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.
Upvotes: 0