双击NSCollectionView

use*_*511 5 cocoa nscollectionview

我正在尝试让我的程序识别出使用NSCollectionView进行双击.我尝试过这个指南:http://www.springenwerk.com/2009/12/double-click-and-nscollectionview.html但是当我这样做时,没有任何反应,因为IconViewBox中的委托是null:

h文件:

@interface IconViewBox : NSBox
{
    IBOutlet id delegate;
}
@end
Run Code Online (Sandbox Code Playgroud)

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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

第二个NSLog永远不会被打印,因为委托是空的.我已经连接了我的nib文件中的所有内容并按照说明操作.有谁知道为什么或替代为什么这样做?

Gra*_*iln 5

您可以通过继承集合项的视图来捕获集合视图项中的多次单击.

  1. 子类NSView并添加一个mouseDown:方法来检测多次点击
  2. 将nib中的NSCollectionItem视图更改NSViewMyCollectionView
  3. collectionItemViewDoubleClick:在相关的实现NSWindowController

这可以通过让NSView子类检测到双击并传递响应者链来实现.collectionItemViewDoubleClick:调用响应程序链中要实现的第一个对象.

通常,您应该collectionItemViewDoubleClick:在关联实现NSWindowController,但它可以在响应链中的任何对象中.

@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
Run Code Online (Sandbox Code Playgroud)


Ext*_*ire 0

尽管您这么说,您需要确保遵循本教程中的第四步:

4. Open IconViewPrototype.xib in IB and connect the View's delegate outlet with "File's Owner":
Run Code Online (Sandbox Code Playgroud)

如果您确实遵循了其余步骤,那就应该可以了。