tags:

views:

101

answers:

2

How to check the items in contextual menu in cocoa i want to place a checkmark next to specific items I was added menu like shown below in mouseDown function, (void)mouseDown:(NSEvent *)event { NSPoint pointInView = [self convertPoint:[event locationInWindow] fromView:nil];

if (NSPointInRect(pointInView, [self shapeRect]) )
{       
    NSMenu *theMenu = [[[NSMenu alloc] initWithTitle:@"default Contextual Menu"] autorelease];

    [theMenu insertItemWithTitle:@"Circle" action:@selector(circle:) keyEquivalent:@"" atIndex:0];
    [theMenu insertItemWithTitle:@"Rectangle" action:@selector(rectangle:) keyEquivalent:@"" atIndex:1];

    [NSMenu popUpContextMenu:theMenu withEvent:event forView:self];


}   
}

how put check marks for this..

+2  A: 

Take a look at the NSUserInterfaceItemValidation protocol. When a menu is displayed, it will query each responder in the responder chain with validateUserInterfaceItem: method to determine if the item should be enabled. (An item will be enabled so long as one responder in the chain returns YES) This also gives you an opportunity to customize the item. For example:

- (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item {
    if ([item action] == @selector(actionMethodForItemThatShouldBeChecked:)] {
        // This method is also used for toolbar items, so it's a good idea to 
        // make sure you're validating a menu item here
        if ([item respondsToSelector:@selector(setState:)])
            [item setState:NSOnState];
    }
    return YES;
}
Alex
how to put check marks for above cenario
sateesh
`setState:` adds the checkmark. Read the documentation.
Alex
A: 

You want something like this:

// Place a check mark next to "Circle"
NSMenuItem * theItem = [theMenu indexOfItemWithTitle: @"Circle"];
[item setState: NSOnState];

You would use NSOffState to remove the check mark.

Daniel Jette