即使调用了所有正确的方法,NSView 的上下文 NSMenu 也永远不会显示
我有一个 NSCollectionView,其中有一堆 NSView,垂直堆叠,使其看起来有点像 UIKit 的 UITableView
。一切都按预期工作,除了一件事:
右键单击任何一个 NSView
时,我希望将 NSMenu
设置为视图的 menu
code> 被显示,但是可惜 - 什么也没发生。
疯狂的部分是所有正确的方法都被调用,正如预期的那样:-rightMouseDown:
、-menuForEvent:
以及最后的-menu
。
当我将任何对象设置为 NSMenu
的 delegate
时,menuWillOpen:
被不调用,所以看起来对我来说,苹果这边的事情发生了故障,就在请求菜单
和实际显示它之间。
有人能解释一下吗?
提前致谢。
附言。就其价值而言,我使用 popUpMenuPositioningItem:atLocation:inView:
手动呈现的 NSMenu
(不依赖 Apple 的右键单击处理) 显示。
编辑/更新/澄清
有问题的 NSCollectionView
位于一个 NSWindow
内,当单击 NSStatusItem
时显示该窗口,例如 CoverSutra/TicToc/what你。来自 MyWindow
NSWindow
子类的一些代码:
- (void)awakeFromNib {
[self setStyleMask:NSBorderlessWindowMask];
[self setExcludedFromWindowsMenu:YES];
}
- (BOOL)canBecomeMainWindow {
return YES;
}
- (BOOL)canBecomeKeyWindow {
return YES;
}
- (BOOL)isMovable {
return NO;
}
- (void)presentFromPoint:(NSPoint)point {
point.y -= self.frame.size.height;
point.x -= self.frame.size.width / 2;
[self setFrameOrigin:point];
[self makeMainWindow];
[self makeKeyAndOrderFront:self];
}
presentFromPoint:
是我用来从我喜欢的任何点呈现它的方法,在我的例子中,只是从在 NSStatusItem
下方。 (与这个问题不太相关)
顺便说一句,我的应用程序的 Info.plist
中的 LSUIElement
设置为 YES
,所以它不显示菜单栏或 Dock 图标。它位于状态栏中,并有一个在单击 NSStatusItem
时显示的窗口。
视图层次结构如下:
MyWindow
=> contentView
=> NSScrollView
=> NSCollectionView
NSCollectionView 有一个 NSCollectionViewItem
子类连接到其 itemPrototype
属性,并且 NSCollectionViewItem
> 子类有一个连接到其 view
属性的 NSView
子类。 反过来,NSView
子类有一个 NSMenu
连接到其 menu
属性。 最后但并非最不重要的一点是:此 NSMenu
内部有一个 NSMenuItem
。
NSCollectionViewItem 子类和 NSView 子类目前都没有做任何有趣的事情,它们只是空子类。
连接到 NSView
的 menu
属性的 NSMenu
是右键单击 NSView
时应显示的内容,但正如我希望我已经明确表示的那样:它实际上并未显示。
更新
我仍然不知道是什么导致了这个问题,但我决定从 NSCollectionView
'继续',因为它并不真正适合我想要做的事情,而且我是现在使用 TDListView
,它的作用就像一个魅力。
I have an NSCollectionView
with a bunch of NSView
s in it, stacked vertically, to make it look a bit like UIKit
's UITableView
. Everything works as expected, except for one thing:
When right-clicking any one of the NSView
s, I expect the NSMenu
I set to be view's menu
to be shown, but alas - nothing happens.
The crazy part is all the right methods are being called, exactly as could be expected: -rightMouseDown:
, -menuForEvent:
and finally -menu
.
When I set up any object as the NSMenu
's delegate
, menuWillOpen:
is not called, so it seems to me something fails over on Apple's side of things, just in between asking for the menu
, and actually showing it.
Would anyone be able to shed a light on this?
Thanks in advance.
PS. For what it's worth, NSMenu
s I present manually (without relying on Apple's right-click handling) using popUpMenuPositioningItem:atLocation:inView:
are shown.
Edit / Update / Clarification
The NSCollectionView
in question is inside an NSWindow
that's being shown when an NSStatusItem
is clicked, like CoverSutra/TicToc/what have you. Some code from the MyWindow
NSWindow
subclass:
- (void)awakeFromNib {
[self setStyleMask:NSBorderlessWindowMask];
[self setExcludedFromWindowsMenu:YES];
}
- (BOOL)canBecomeMainWindow {
return YES;
}
- (BOOL)canBecomeKeyWindow {
return YES;
}
- (BOOL)isMovable {
return NO;
}
- (void)presentFromPoint:(NSPoint)point {
point.y -= self.frame.size.height;
point.x -= self.frame.size.width / 2;
[self setFrameOrigin:point];
[self makeMainWindow];
[self makeKeyAndOrderFront:self];
}
presentFromPoint:
is the method I use to present it from any point I like, in my case from just below the NSStatusItem
. (Not really relevant to this problem)
My application has LSUIElement
in its Info.plist
set to YES
by the way, so it doesn't show a menu bar or a Dock icon. It lives in the status bar, and has a window that's shown when the NSStatusItem
is clicked.
The view hierarchy is as follows:
MyWindow
=> contentView
=> NSScrollView
=> NSCollectionView
The NSCollectionView
has an NSCollectionViewItem
subclass connected to its itemPrototype
property, and the NSCollectionViewItem
subclass has an NSView
subclass connected to its view
property.
The NSView
subclass, in turn, has an NSMenu
connected to its menu
property.
And last but not least: This NSMenu
has one NSMenuItem
sitting inside it.
Both the NSCollectionViewItem
subclass and the NSView
subclass do nothing interesting as of now, they're just empty subclasses.
The NSMenu
connected to the NSView
's menu
property is what should be shown when the NSView
is right-clicked, but as I hope I have made clear: It isn't actually shown.
Update
I still have no idea what caused this problem, but I've decided to 'move on' from NSCollectionView
, as it wasn't really fit for what I was trying to do anyway, and I am now using TDListView
which works like a charm.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论