I've bounded the menu to the NSCollectionView in interface builder. But when I CTRL+click (right click) on it the menu is not showing.
I've tried adding some method to the NSCollectionView subclass. None of them is invoked:
+ (NSMenu*)defaultMenu
- (NSMenu *)menuForEvent:(NSEvent *)theEvent
- (void)rightMouseDown:(NSEvent *)theEvent
- (void)sendEvent:(NSEvent *)theEvent
The only method which is invoked is:
- (NSView *)hitTest:(NSPoint)aPoint
Which means that the NSCollectionView receives the mouse events.
I've also tried to add the same methods to the subclass of NSCollectionViewItem, and the result is the same. Only hitTest:
is called.
This works for me:
@interface MyCollectionView : NSView
-(void)mouseDown:(NSEvent *)theEvent;
@end
@implementation MyCollectionView
-(void)mouseDown:(NSEvent *)theEvent
{
NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];
[theMenu insertItemWithTitle:@"Beep" action:@selector(beep) keyEquivalent:@"" atIndex:0];
[theMenu insertItemWithTitle:@"Honk" action:@selector(honk) keyEquivalent:@"" atIndex:1];
[NSMenu popUpContextMenu:theMenu withEvent:theEvent forView:self];
[super mouseDown:theEvent];
}
-(void)beep{
}
-(void)honk{
}
@end
I hope this helps.