Custom view in NSMenuItem disables the NSPopUpButton selection

2.3k views Asked by At

I want to customize an an NSPopUpButton so I have implemented an CustomMenuItemView which right now only has the following code (for testing purposes):

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor redColor] set];
    NSRectFill(dirtyRect);
}

Now, for every NSMenuItem i add to the NSMenu in myPopUpButton.menu I set the view to my custom view:

NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"Some title" action:NULL keyEquivalent:@""];
menuItem.view = [[CustomMenuItemView alloc] initWithFrame:NSMakeRect(0, 0, 100, 25)];

When I run my program and open the popup button the menuitem selection seems disabled (i.e. nothing happens when I click on it).

I am guessing that it is not actually disabled; it just doesn't respond to events anymore. Do I need to add some event handling in my custom view? If so, how?

1

There are 1 answers

1
pajevic On BEST ANSWER

I solved the problem by adding the mouseUp method to my CustomMenuItemView:

- (void)mouseUp:(NSEvent*) event
{
    NSMenu *menu = self.enclosingMenuItem.menu;
    [menu cancelTracking];
    [menu performActionForItemAtIndex:[menu indexOfItem:self.enclosingMenuItem]];
}