问题描述
我有一个NSMenu弹出一个NSStatusItem使用popUpStatusItemMenu。这些NSMenuItems显示一堆不同的链接,每个都与setAction:连接到目标的openLink:方法。这种安排已经工作很长时间了。不幸的是,我最近决定尝试使用NSMenuItem的setView:方法来提供一个更好的方法来处理它。 / slicker接口。基本上,我只是停止设置标题,创建NSMenuItem,然后使用setView:显示自定义视图。这完美地工作,菜单项目看起来不错,显示我的自定义视图。
但是,当用户选择一个菜单项并释放鼠标时, (即openLink:未调用)。如果我只是简单地注释掉setView:调用,然后动作再次工作(当然,菜单项是空白,但动作执行正确)。我的第一个问题是,为什么设置视图打破了NSMenuItem的操作。
没有问题,我想,我会通过检测我的自定义查看并调用我的action方法。我添加了这个方法到我的自定义视图:
- (void)mouseUp:(NSEvent *)theEvent {
NSLog (@in mouseUp);
}
没有骰子!这个方法从不被调用。
我可以设置跟踪rects和接收mouseEntered:事件。我在我的mouseEntered例程中做了几个测试,如下:
if([[self window] ignoresMouseEvents]){NSLog @忽略鼠标事件); }
else {NSLog(@不忽略鼠标事件); }
if([[self window] canBecomeKeyWindow]){dNSLog((@canBecomeKeyWindow)); }
else {NSLog(@not canBecomeKeyWindow); }
if([[self window] isKeyWindow]){dNSLog((@isKeyWindow)); }
else {NSLog(@not isKeyWindow); }
得到以下回应:
不忽略鼠标事件
canBecomeKeyWindow
not isKeyWindow
$ b b
这是问题吗? not isKeyWindow?大概这是不好的,因为苹果的文档说如果用户点击一个不在关键窗口中的视图,默认情况下,窗口被提出并使键,但鼠标事件不分派。但是必须有一种方法来检测这些事件。如何?
添加:
[[self window] makeKeyWindow] ;
没有效果,尽管canBecomeKeyWindow为YES。
code> - (void)mouseUp:(NSEvent *)event {
NSMenuItem * mitem = [self enclosingMenuItem];
NSMenu * m = [mitem menu];
[m cancelTracking];
[m performActionForItemAtIndex:[m indexOfItem:mitem]];
}
但是我有keyhandling的问题,如果你解决了这个问题,可以去我的问题,帮助我一点。
I have an NSMenu popping out of an NSStatusItem using popUpStatusItemMenu. These NSMenuItems show a bunch of different links, and each one is connected with setAction: to the openLink: method of a target. This arrangement has been working fine for a long time. The user chooses a link from the menu and the openLink: method then deals with it.
Unfortunately, I recently decided to experiment with using NSMenuItem's setView: method to provide a nicer/slicker interface. Basically, I just stopped setting the title, created the NSMenuItem, and then used setView: to display a custom view. This works perfectly, the menu items look great and my custom view is displayed.
However, when the user chooses a menu item and releases the mouse, the action no longer works (i.e., openLink: isn't called). If I just simply comment out the setView: call, then the actions work again (of course, the menu items are blank, but the action is executed properly). My first question, then, is why setting a view breaks the NSMenuItem's action.
No problem, I thought, I'll fix it by detecting the mouseUp event in my custom view and calling my action method from there. I added this method to my custom view:
- (void)mouseUp:(NSEvent *)theEvent {
NSLog(@"in mouseUp");
}
No dice! This method is never called.
I can set tracking rects and receive mouseEntered: events, though. I put a few tests in my mouseEntered routine, as follows:
if ([[self window] ignoresMouseEvents]) { NSLog(@"ignoring mouse events"); }
else { NSLog(@"not ignoring mouse events"); }
if ([[self window] canBecomeKeyWindow]) { dNSLog((@"canBecomeKeyWindow")); }
else { NSLog(@"not canBecomeKeyWindow"); }
if ([[self window] isKeyWindow]) { dNSLog((@"isKeyWindow")); }
else { NSLog(@"not isKeyWindow"); }
And got the following responses:
not ignoring mouse events
canBecomeKeyWindow
not isKeyWindow
Is this the problem? "not isKeyWindow"? Presumably this isn't good because Apple's docs say "If the user clicks a view that isn’t in the key window, by default the window is brought forward and made key, but the mouse event is not dispatched." But there must be a way do detect these events. HOW?
Adding:
[[self window] makeKeyWindow];
has no effect, despite the fact that canBecomeKeyWindow is YES.
Add this method to your custom NSView and it will work fine with mouse events
- (void)mouseUp:(NSEvent*) event {
NSMenuItem* mitem = [self enclosingMenuItem];
NSMenu* m = [mitem menu];
[m cancelTracking];
[m performActionForItemAtIndex: [m indexOfItem: mitem]];
}
But i'm having problems with keyhandling, if you solved this problem maybe you can go to my question and help me a little bit.
这篇关于NSMenuItem中的自定义NSView不接收鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!