tags:

views:

131

answers:

2

Thanks Chuck for the quick reply. I am having a new problem now. my contextual menu is getting displayed but the menu items are not activated. so my new code for displaying the menu is as follows:

NSMenu *defMenu = [[[NSMenu alloc] initWithTitle:@"default Contextual Menu"] autorelease];

[defMenu insertItemWithTitle:@"Open" action:@selector(openFile) keyEquivalent:@"" atIndex:0];

[defMenu insertItemWithTitle:@"Delete" action:@selector(deleteFile) keyEquivalent:@"" atIndex:1];

return defMenu;

and function declaratons of deleteFile and openFile are as follows:

-(int)openFile;

-(int)deleteFile;

and i am calling my contextual menu as follows:

-(void)doSingleClick {

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

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

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

 return;
}

}

my contextual menu items are all shaded and cannot be clicked. Please can you tell where i am going wrong.

Thanks

+1  A: 

You can't define such an action. An action is a method that takes one object argument representing the object that triggered the action message. You need to create an action in your controller that calls through to the underlying openFile: method.

Chuck
Thanks chuck for the reply. I am having a new problem i.e. my menuitems are not getting activated. I have modified my question. Please can you tell me where i am going wrong?Thanks
King
A: 

Your openFile: method takes an int as a parameter. Since insertItemWithTitle:action:withObject:keyEquivalent:atIndex: takes an object, the selector you give it must also take an object.

You can use NSNumber to wrap your int as an object, and simply change your openFile: method to take an NSNumber rather than an int. Like so:

[defMenu insertItemWithTitle:@"Open" action:@selector(openFile:) withObject:[NSNumber numberWithInt:5] keyEquivalent:@"" atIndex:0];

- (void)openFile:(NSNumber *)fileNumber {
    int rowClicked = [fileNumber intValue];
    // Do whatever your old method did here
}

EDIT: To answer your updated question:

The reason your menu items are disabled is that you've only told them what method name to call. You never told the items on which object instance those methods should actually be called. To fix this, you need to set the items' target:

NSMenuItem *openItem = [defMenu insertItemWithTitle:@"Open" action:@selector(openFile:) withObject:[NSNumber numberWithInt:5] keyEquivalent:@"" atIndex:0];
[openItem setTarget:self];

And so forth for each item you've got.

Matt Ball
Thanks Matt for the reply. Actually after taking a look at chucks reply, i modified my funcs so that they dont take any arguments. But now I am having a new problem i.e. my menuitems are not getting activated. I have modified my question. Please can you tell me where i am going wrong? Thanks
King
My answer's been updated to address that. In the future, you really shouldn't edit a question to drastically change its intent, since it makes the answers which were posted before the edit somewhat nonsensical. If the original question is answered, but an unrelated issue comes up as a result, just ask another question!
Matt Ball
Thanks Matt for the reply.
King