How do I display an image in an NSMenuItem?
All right, in Lodestone’s LSToolGroup, I have the following method:
-(void)setUpMenu { NSEnumerator *e = [tools objectEnumerator]; LSTool *tool; [menu removeAllItems];
while(tool = [e nextObject])
{
NSMenuItem *mi = [menu addItemWithTitle:[tool name] action:@selector(selectTool:) keyEquivalent:@""];
[mi setTarget:self];
[mi setImage:[tool icon]];
[mi setRepresentedObject:tool];
}
selectedTool = [tools objectAtIndex:0]; }
The only really interesting thing to note there is [mi setImage:[tool icon]]–what you’re not seeing is that LSTool’s icon implementation is just using [NSImage imageWithName:…]. Easy!
I thought I’d note that even though addItemWithTitle:… returns id
WARNING: The NSMenuItem protocol is being removed from the Application Kit; you must use the NSMenuItem class instead. This change does not affect binary compatibility between different versions of projects, but might cause failures in project builds. To adapt your projects to this change, alter all references to the protocol (for example,