views:

757

answers:

4

hi guys

i am having a problem displaying context sensitive menu on control click on a tableview when multiple rows are selected. Its working fine when a single row is selected and then control clicked on it. The way i am implementing this is shown below:

-(void)doSingleClick  
{  
    NSLog(@"single clicked");

    if([[NSApp currentEvent] modifierFlags] & NSControlKeyMask)
    {

     NSLog(@"control clicked.......");

     [NSMenu popUpContextMenu:[self showContextMenu] withEvent:[NSApp currentEvent] forView:tableView];

        return;
    }

}

and showContextMenu function returns a NSMenu object.

I am dong it this way as my table view for some strange reason does not recognize mouseDown or mouseUp or menuForEvent events.

the problem with the above code segment is, when multiple rows are selected and control clicked, it does not recognize the control click and does not go into that loop and hence not displaying the context menu.

Please suggest me a mechanism to achieve this.

Thanks

A: 

I don't believe the action method is called when multiple rows are selected.

What would probably be a lot easier would be to override the menuForEvent: method in NSTableView. You'd have to create a subclass of NSTableView to do this, but it would be a cleaner solution.

You could also create an informal protocol (a category on NSObject) and have the NSTableView delegate return the appropriate menu.

@interface NSObject (NSCustomTableViewDelegate)

- (NSMenu *)tableView:(NSTableView *)tableView menuForEvent:(NSEvent *)event;

@end

@implementation NSObject (NSCustomTableViewDelegate)

- (NSMenu *)tableView:(NSTableView *)tableView menuForEvent:(NSEvent *)event {
    return nil;
}

@end

And in your NSTableView subclass:

- (NSMenu *)menuForEvent:(NSEvent *)event {
    return [[self delegate] tableView:self menuForEvent:event];
}
Alex
i hve tableviewcontroller class which is a subclass of NSTableView. but this class in which i implemented menuForEvent method but its not getting called for some reason. please can u tell me where i am doing wrong??Thanks
King
What else have you implemented that would intercept a click? It's possible this isn't getting called because the click is being intercepted elsewhere.
Alex
nothing else. i have implemented [tableView setAction:@selector(doSingleClick)]; in awakeFromNib and[tableView setDoubleAction:@selector(doSingleClick)]; in awakeFromNib to intercept double clicks.I tried using mouseDown and other mouse events without these methods but they were not getting called or the app is not recognizing these events.But the thing is even if there are multiple rows selected, and then i control click, it is going into doSingleClick func but not into the loop which is for control click.thanks
King
sorry it is [tableView setDoubleAction:@selector(doDoubleClick)]; for doubleclick operation.
King
Alex
Thanks Alex for the comments. The point is for some strange reason that i dont know,my app is not able to recognise the menuforevent method( i.e. it is not going into that func and i did that without assigning any selector to the table view's action. ).the if statement is working because if only one file is selected, it is showing me the context sensitive menu.Thanks
King
A: 

i hve tableviewcontroller class which is a subclass of NSTableView.

That's very bad naming and suggests that you are not architecting your application properly. Views aren't controllers. Keep them separate.

but this class in which i implemented menuForEvent method but its not getting called for some reason.

Did you make your table view an instance of this class in Interface Builder? If not, your instance is still an NSTableView, and the subclass you wrote is what Ian Hickson might call “a work of fiction”.

Peter Hosey
A: 

Howdy all, I don't recommend the approach that is given in the answers above. Instead, look at the "DragNDropOutlineView" example in Leopard and higher. That, and the release notes, give a proper way to implement contextual menus for a single row, or multiple rows. This includes having AppKit automatically do the proper highlighting.

corbin dunn (NSTableView Software Engineer)

corbin dunn
A: 

Corbin's answer is the best one here.

link text

Gary