简体   繁体   English

NSMenuItem,自定义视图和mouseUp的怪异问题:

[英]Weird issue with NSMenuItem, custom view and mouseUp:

I'm having a very very strange issue here with a NSMenu. 我在使用NSMenu时遇到一个非常非常奇怪的问题。

About half the NSMenuItems I use have custom views on them through the setView: method on NSMenuItem. 我使用的大约一半的NSMenuItem通过NSMenuItem的setView:方法对其具有自定义视图。 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. 在此自定义视图中,我实现了mouseUp:来捕获用户单击菜单项的时间,并且在我第一次打开菜单时可以完美地工作。

The second time though, the mouseUp doesn't get called on any of these menu items if I hold the mouse steady when clicking. 但是,如果我第二次单击鼠标不放,则不会在任何这些菜单项上调用mouseUp。 However, if I click down, then move the cursor ever so slightly, and release the mouseUp gets called. 但是,如果我单击鼠标左键,则将光标稍微移动一点,然后释放mouseUp即可。 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. 因此,由于某种原因,某些东西正在拦截这些事件,但是只有第二次出现菜单时,如果光标在mouseDown事件之后移动,菜单就会通过。 (For some reason mouseDown never gets called though, on the first or second appearance of the menu). (由于某种原因,在菜单的第一次或第二次出现时,mouseDown从未被调用)。

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? 是什么在拦截我的鼠标事件,为什么它们在菜单的第一个外观上传递给我的自定义视图,而在第二个菜单上却没有传递?

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: 将此方法添加到我在菜单项内使用的NSView子类中解决了该问题:

- (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 有关更多背景信息,请查看以下链接: http : //openradar.appspot.com/7128269

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM