Add website source.
[enjoyable.git] / Categories / NSMenu+RepresentedObjectAccessors.m
1 //
2 // NSMenu+RepresentedObjectAccessors.m
3 // Enjoyable
4 //
5 // Created by Joe Wreschnig on 3/4/13.
6 //
7 //
8
9 #import "NSMenu+RepresentedObjectAccessors.h"
10
11 @implementation NSMenu (RepresentedObjectAccessors)
12
13 - (NSMenuItem *)itemWithRepresentedObject:(id)object {
14 for (NSMenuItem *item in self.itemArray)
15 if ([item.representedObject isEqual:object])
16 return item;
17 return nil;
18 }
19
20 - (void)removeItemWithRepresentedObject:(id)object {
21 NSInteger idx = [self indexOfItemWithRepresentedObject:object];
22 if (idx != -1)
23 [self removeItemAtIndex:idx];
24 }
25
26 - (NSMenuItem *)lastItem {
27 return self.itemArray.lastObject;
28 }
29
30 - (void)removeLastItem {
31 if (self.numberOfItems)
32 [self removeItemAtIndex:self.numberOfItems - 1];
33 }
34
35 @end
36
37 @implementation NSPopUpButton (RepresentedObjectAccessors)
38
39 - (NSMenuItem *)itemWithRepresentedObject:(id)object {
40 return [self.menu itemWithRepresentedObject:object];
41 }
42
43 - (void)selectItemWithRepresentedObject:(id)object {
44 [self selectItemAtIndex:[self indexOfItemWithRepresentedObject:object]];
45 }
46
47
48 @end