NSRect frame = [(NSButton *)sender frame];
NSPoint menuOrigin = [[(NSButton *)sender superview] convertPoint:NSMakePoint(frame.origin.x, frame.origin.y+frame.size.height+40)
toView:nil];
NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown
location:menuOrigin
modifierFlags:NSLeftMouseDownMask // 0x100
timestamp:nil
windowNumber:[[(NSButton *)
sender window] windowNumber]
context:[[(NSButton *)
sender window] graphicsContext]
eventNumber:0
clickCount:1
pressure:1];
NSMenu *menu = [[NSMenu alloc] init];
[menu insertItemWithTitle:@"add"
action:@selector(add:)
keyEquivalent:@""
atIndex:0];
[NSMenu popUpContextMenu:menu withEvent:event forView:(NSButton *)
sender];
September 14, 2009 at 10:02 pm
Hi.
I created Cocoa Application and add instance of NSButton. I add IBAction method to this button and inside I put your sample code shown above. When I click on button context menu pops up but is not active, I can not select the “add” item because it is disabled. How to make this context menu active??
thanks
October 8, 2009 at 1:59 pm
After creating the menu above, call the following to enable the menu items:
[menu setAutoenablesItems:NO];