2 // NSMenu+RepresentedObjectAccessors.m
5 // Created by Joe Wreschnig on 3/4/13.
9 #import "NSMenu+RepresentedObjectAccessors.h"
11 @implementation NSMenu (RepresentedObjectAccessors)
13 - (NSMenuItem *)itemWithRepresentedObject:(id)object {
14 for (NSMenuItem *item in self.itemArray)
15 if ([item.representedObject isEqual:object])
20 - (NSMenuItem *)itemWithIdenticalRepresentedObject:(id)object {
21 for (NSMenuItem *item in self.itemArray)
22 if (item.representedObject == object)
27 - (NSMenuItem *)lastItem {
28 return self.itemArray.lastObject;
31 - (void)removeLastItem {
32 if (self.numberOfItems)
33 [self removeItemAtIndex:self.numberOfItems - 1];
38 @implementation NSPopUpButton (RepresentedObjectAccessors)
40 - (NSMenuItem *)itemWithRepresentedObject:(id)object {
41 return [self.menu itemWithRepresentedObject:object];
44 - (NSMenuItem *)itemWithIdenticalRepresentedObject:(id)object {
45 return [self.menu itemWithIdenticalRepresentedObject:object];
48 - (void)selectItemWithRepresentedObject:(id)object {
49 [self selectItemAtIndex:[self indexOfItemWithRepresentedObject:object]];
52 - (void)selectItemWithIdenticalRepresentedObject:(id)object {
53 NSMenuItem *item = [self.menu itemWithIdenticalRepresentedObject:object];
54 [self selectItem:item];