我试图让我的程序识别 NSCollectionView 的双击。我尝试遵循本指南:http://www.springenwerk.com/2009/12/double-click-and-nscollectionview.html但是当我这样做时,什么也没有发生,因为 IconViewBox 中的委托(delegate)为空:
.h 文件:
@interface IconViewBox : NSBox
{
IBOutlet id delegate;
}
@end
m 文件:
@implementation IconViewBox
-(void)mouseDown:(NSEvent *)theEvent {
[super mouseDown:theEvent];
// check for click count above one, which we assume means it's a double click
if([theEvent clickCount] > 1) {
NSLog(@"double click!");
if(delegate && [delegate respondsToSelector:@selector(doubleClick:)]) {
NSLog(@"Runs through here");
[delegate performSelector:@selector(doubleClick:) withObject:self];
}
}
}
第二个 NSLog 永远不会被打印,因为 delegate 为 null。我已连接 nib 文件中的所有内容并按照说明进行操作。有谁知道为什么或替代为什么这样做?
最佳答案
您可以通过对集合项的 View 进行子类化来捕获 Collection View 项中的多次点击。
- 子类
NSView
并添加mouseDown:
方法来检测多次点击 - 将 Nib 中 NSCollectionItem 的 View 从
NSView
更改为MyCollectionView
- 在关联的
NSWindowController
中实现collectionItemViewDoubleClick:
这是通过让 NSView
子类检测双击并传递响应者链来实现的。响应程序链中第一个实现 collectionItemViewDoubleClick:
的对象被调用。
通常,您应该在关联的 NSWindowController
中实现 collectionItemViewDoubleClick:
,但它可以在响应程序链内的任何对象中。
@interface MyCollectionView : NSView
/** Capture double-clicks and pass up responder chain */
-(void)mouseDown:(NSEvent *)theEvent;
@end
@implementation MyCollectionView
-(void)mouseDown:(NSEvent *)theEvent
{
[super mouseDown:theEvent];
if (theEvent.clickCount > 1)
{
[NSApplication.sharedApplication sendAction:@selector(collectionItemViewDoubleClick:) to:nil from:self];
}
}
@end
关于cocoa - 双击 NSCollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14574334/