NSMenuItem,自定义视图和mouseUp的怪异问题: [英] Weird issue with NSMenuItem, custom view and mouseUp:

查看:98
本文介绍了NSMenuItem,自定义视图和mouseUp的怪异问题:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用NSMenu时遇到了一个非常非常奇怪的问题.

I'm having a very very strange issue here with a NSMenu.

我使用的大约一半的NSMenuItem通过NSMenuItem的setView:方法对其具有自定义视图.在此自定义视图中,我实现了mouseUp:来捕获用户单击菜单项的时间,并且在我第一次打开菜单时可以完美地工作.

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.

但是,如果我第二次单击鼠标并保持稳定,则第二个菜单项都不会调用mouseUp.但是,如果我单击鼠标左键,则将光标稍稍移动一下,然后释放mouseUp即可.因此,由于某种原因,某些东西正在拦截这些事件,但是仅第二次出现菜单,并且如果在mouseDown事件之后光标移动,菜单就会通过. (由于某种原因,在菜单的第一次或第二次显示时,mouseDown从未被调用).

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?

推荐答案

我遇到了同样的问题.原来问题出在第一次单击菜单后,我正在启动外部应用程序,并且再次打开菜单时,其窗口不再是键.将此方法添加到我在菜单项内使用的NSView子类中解决了该问题:

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];
}

有关更多背景信息,请查看以下链接: http://openradar.appspot.com/7128269

For more context, have a look at this link: http://openradar.appspot.com/7128269

这篇关于NSMenuItem,自定义视图和mouseUp的怪异问题:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆