即使调用了所有正确的方法,NSView 的上下文 NSMenu 也永远不会显示

发布于 2024-10-16 21:46:58 字数 2562 浏览 1 评论 0原文

我有一个 NSCollectionView,其中有一堆 NSView,垂直堆叠,使其看起来有点像 UIKit 的 UITableView。一切都按预期工作,除了一件事:

右键单击任何一个 NSView 时,我希望将 NSMenu 设置为视图的 menu code> 被显示,但是可惜 - 什么也没发生。

疯狂的部分是所有正确的方法都被调用,正如预期的那样:-rightMouseDown:-menuForEvent: 以及最后的-menu

当我将任何对象设置为 NSMenudelegate 时,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 子类目前都没有做任何有趣的事情,它们只是空子类。

连接到 NSViewmenu 属性的 NSMenu 是右键单击 NSView 时应显示的内容,但正如我希望我已经明确表示的那样:它实际上并未显示。

更新

我仍然不知道是什么导致了这个问题,但我决定从 NSCollectionView '继续',因为它并不真正适合我想要做的事情,而且我是现在使用 TDListView ,它的作用就像一个魅力。

I have an NSCollectionView with a bunch of NSViews 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 NSViews, 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, NSMenus 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文