【发布时间】:2026-01-17 10:40:01
【问题描述】:
(注意:问题has been asked before,一年多以前的结果还没有定论。希望我们现在了解更多并可以解决它?)
我试图在双击 NSCollectionItem 时在 NSPopover 上显示它。我的 NSCollectionView 设置正确并接收输入。
(如果我将代码复制粘贴到其他 NSView 子类,它工作得很好。NSCollectionView 有一些东西似乎把事情搞砸了。)
代码:
BIECollectionViewItem.h
#import <Cocoa/Cocoa.h>
@class BIEAppDelegate;
@interface BIECollectionViewItem : NSView {
IBOutlet NSPopover *popover;
}
@end
BIECollectionViewItem.m
#import "BIECollectionViewItem.h"
@implementation BIECollectionViewItem
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
}
- (void)mouseDown:(NSEvent *)theEvent {
[super mouseDown: theEvent];
if([theEvent clickCount] == 2){
[popover showRelativeToRect: [self bounds] ofView: self preferredEdge: NSMaxYEdge];
}
}
@end
如果我在 mouseDown 事件中添加日志语句,我可以确认该事件正在被触发。但是,NSPopover 拒绝出现!
有谁知道如何让 NSPopover 出现在 NSCollectionView 上?
【问题讨论】:
-
你在哪里
alloc和initNSPopover? -
我不是; Interface Builder 正在为我处理这个问题。我刚刚从 IB 库中拖了一个
NSPopover。然后它自动创建了一个NSPopover对象、一个NSViewController(标记为“Popover View Controller”)和一个NSView,其中包含我希望出现在popover 中的界面项。我已经在主窗口中测试过了,一切正常,但是当我把它放到NSCollectionView时,它就没有出现。
标签: objective-c nscollectionview nscollectionviewitem nspopover