问题
I'm having a very very strange issue here with a NSMenu.
About half the NSMenuItems I use have custom views on them through the setView: method on NSMenuItem. In this custom view I've implemented mouseUp: to catch when the user clicks on the menu item, and this works perfectly the first time I open the menu.
The second time though, the mouseUp doesn't get called on any of these menu items if I hold the mouse steady when clicking. However, if I click down, then move the cursor ever so slightly, and release the mouseUp gets called. So for some reason something is intercepting these events, but only the second time the menu comes up, and it gets through if the cursor moves after the mouseDown event. (For some reason mouseDown never gets called though, on the first or second appearance of the menu).
Anybody got any clue as to what might be going on here? What is intercepting my mouse events, and why are they getting passed through to my custom view on the first appearance of the menu, but not on the second?
回答1:
I had the same problem. Turns out the issue was I was launching an external application after the first menu click, and when the menu was opened again its window was no longer key. Adding this method to the NSView subclass I'n using inside the menu items fixed the problem:
- (void)viewWillMoveToWindow:(NSWindow *)newWindow;
{
[super viewWillMoveToWindow:newWindow];
if ( newWindow != nil && ![newWindow isKeyWindow] )
[newWindow becomeKeyWindow];
[self updateTrackingAreas];
}
For more context, have a look at this link: http://openradar.appspot.com/7128269
来源:https://stackoverflow.com/questions/15075033/weird-issue-with-nsmenuitem-custom-view-and-mouseup