How do you add context senstive menu to NSOutlineView (ie right click menu)

前端 未结 5 1995
终归单人心
终归单人心 2020-12-25 14:16

How do you add the ability to right click on a row in an NSOutlineView so you can say delete an object or some other activity. (ie Like when you right click on a folder in t

5条回答
  •  鱼传尺愫
    2020-12-25 15:01

    Much later than the OP question, but for others like me wondering, here is my solution. It also needs subclassing NSOutlineView, which is not encouraged by Apple doc, anyway…

    Rather than override menuForEvent: I override rightMouseDown:

    - (void)rightMouseDown:(NSEvent *)event {
        NSPoint pt = [self convertPoint:[event locationInWindow] fromView:nil];
        NSInteger row = [self rowAtPoint:pt];
        id item = [self itemAtRow:row];
        NSMenu *menu;
        //set the menu to one you have defined either in code or IB through outlets
        self.menu = menu;
        [super rightMouseDown:event];
    }
    

    This has the advantage of keeping delegate calls to update the menu thereafter and also keeps row outlining on right click.

提交回复
热议问题