Search code examples
objective-ccocoansoutlineview

NSOutlineView how to connect context menu to the delegate


To show a context menu on right click of an NSOutlineView you have to subclass it and override:

- (NSMenu *)menuForEvent:(NSEvent *)theEvent {
    NSPoint pt = [self convertPoint:[theEvent locationInWindow] fromView:nil];
    id item = [self itemAtRow: [self rowAtPoint:pt]];

    // Only the delegate knows how to create a menu based on the item
    return [self createMenuFor: item]; 
}

So what is the proper way to get the delegate to create the menu here and handle the menu actions?

EDIT - So the subclass doesn't know how to create the menu so I just did this. Is it ok? I just didn't see any example code for how to communicate with the delegate online.

    return [[self delegate] createMenuForItem:item]; 

And obviously defined a method in my outline view delegate to return an NSMenu.


Solution

  • First, you should be calling super for any events except when ([theEvent type] == NSRightMouseDown). I'm not sure this is your problem but it's a good place to start.

    Second, this isn't a delegate method, so it has nothing to do with connecting any delegate. You can safely ignore that.

    Third, are you sure you've set the outline view's class name to that of your subclass (the subclass in which you implemented the above) in Interface Builder or (if you created it in code) specified your new subclass in code?