Contextual menu on NSOutlineView without subclassing

  • Hi all,

    is there an easy way to implement a contextual menu for an
    NSTableview/NSOutlineView without subclassing? (a menuForEvent
    delegate method would be fantastic).

    Thanks a lot,

    Bert
  • You can override -menuForEvent: in a category of either NSTableView or
    NSOutlineView, taking advantage of the fact that neither class
    currently overrides NSView's implementation.

    @implementation NSOutlineView(MyExtensions)

    - (NSMenu *) menuForEvent:(NSEvent *)event
      {
        id delegate = [self delegate];

        if ([delegate
    respondsToSelector:@selector(extendMenu:forEvent:inOutlineView:)]) {
          NSMenu *menu = [[[NSMenu alloc] init] autorelease];
          [delegate extendMenu:menu forEvent:event inOutlineView:self];
          return menu;
        }

        return [super menuForEvent:event];
      }

    @end

    Of course this technique may break in a future release...

    dave

    > is there an easy way to implement a contextual menu for an
    > NSTableview/NSOutlineView without subclassing? (a menuForEvent
    > delegate method would be fantastic).
    >
  • Wonderfull. In fact, (shame on me), I never knew you could use
    categories to override behavior. Thought it was only to add methods…

    Thanks,

    Bert

    On 07 Oct 2007, at 20:53, David Spooner wrote:

    >
    > You can override -menuForEvent: in a category of either NSTableView
    > or NSOutlineView, taking advantage of the fact that neither class
    > currently overrides NSView's implementation.
    >
    > @implementation NSOutlineView(MyExtensions)
    >
    > - (NSMenu *) menuForEvent:(NSEvent *)event
    > {
    > id delegate = [self delegate];
    >
    > if ([delegate respondsToSelector:@selector
    > (extendMenu:forEvent:inOutlineView:)]) {
    > NSMenu *menu = [[[NSMenu alloc] init] autorelease];
    > [delegate extendMenu:menu forEvent:event inOutlineView:self];
    > return menu;
    > }
    >
    > return [super menuForEvent:event];
    > }
    >
    > @end
    >
    > Of course this technique may break in a future release...
    >
    > dave
    >
    >
    >> is there an easy way to implement a contextual menu for an
    >> NSTableview/NSOutlineView without subclassing? (a menuForEvent
    >> delegate method would be fantastic).
    >>
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today