17

In a contextual menu, how can I give an NSMenuItem a check mark? I want to place it next to specific items. I create the menu in the mouseDown: function, as shown below:

-(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 can I give the items a check mark?

2
  • Please clarify what you mean by "check". Do you mean you want to place a checkmark next to specific items, or that you want to test for the available items in the menu? Feb 1, 2010 at 12:48
  • i want to place a checkmark next to specific items thanks for quick reply
    – sateesh
    Feb 1, 2010 at 12:49

5 Answers 5

21

Take a look at the NSUserInterfaceItemValidations 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:NSControlStateValueOn];
    }
    return YES;
}
3
  • 1
    This seems abusive of the responder chain system
    – Ky -
    Nov 15, 2016 at 15:00
  • @Ky-: <developer.apple.com/library/archive/documentation/Cocoa/…>: "validateMenuItem: is also a good place to toggle titles or set state on menu items to make sure they're always correct." That's exactly what the system is for. Though I suppose the docs suggest using validateMenuItem: instead of validateUserInterfaceItem: if you're going to adjust state.
    – thakis
    Jan 15 at 16:01
  • @thakis right; that's why I upvoted DavidA's answer and didn't even comment there. It's more in-line with how AppKit was designed. Though, I'd recommend just setting the state with whatever direct reference you have, or using tags, rather than selectors
    – Ky -
    Jan 19 at 20:51
11

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.

1
  • 3
    I think that checking for the menu-item's title is a bad idea. Wouldn't it be better to check what it's action is? The action wouldn't be as likely to change as the menu-item's title would be.
    – Kaydell
    Oct 15, 2014 at 21:25
8

Using the NSMenuValidation protocol you can do this:

-(BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
    if(menuItem.action==@selector(actionMethodForItemThatShouldBeChecked:))
    {
        menuItem.state=NSControlStateValueOn;
    }

    return YES;
}
6

I believe that NSOnState is deprecated (first deprecated in macOS 10.14), you can use NSControlStateValueOn instead. Ex:

[myItem setState: NSControlStateValueOn];

For more information check here

6

In Swift you can check an NSMenuItem like this:

let myItem: NSMenuItem = ...
myItem.state = .on

For more information check https://developer.apple.com/documentation/appkit/nscontrol/statevalue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.