Cocoa 在NSCollectionView中双击

Cocoa 在NSCollectionView中双击,cocoa,nscollectionview,Cocoa,Nscollectionview,我正试图让我的程序识别带有NSCollectionView的双击。我尝试过遵循此指南:但当我这样做时,什么也不会发生,因为IconViewBox中的委托为null: h文件: @interface IconViewBox : NSBox { IBOutlet id delegate; } @end m文件: @implementation IconViewBox -(void)mouseDown:(NSEvent *)theEvent { [super mouseDown:t

我正试图让我的程序识别带有NSCollectionView的双击。我尝试过遵循此指南:但当我这样做时,什么也不会发生,因为IconViewBox中的委托为null:

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永远不会打印,因为委托为null。我已经连接了nib文件中的所有内容,并按照说明进行了操作。有人知道为什么要这样做吗?或者有人知道为什么要这样做吗?

尽管如此,您仍需要确保遵循了本教程中的第四步:

4. Open IconViewPrototype.xib in IB and connect the View's delegate outlet with "File's Owner":

如果您确实遵循了其余步骤,您应该可以这样做。

您可以通过将集合项目的视图子类化来捕获集合视图项目中的多次单击

  • 子类
    NSView
    并添加一个
    mouseDown:
    方法来检测多次单击
  • 将nib中NSCollectionItem的视图从
    NSView
    更改为
    MyCollectionView
  • 在关联的
    NSWindowController中双击:
    ,实现
    CollectionItemView
  • 这是通过让
    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
    

    另一个选项是覆盖
    NSCollectionViewItem
    ,并添加
    nsClickgestureRecognitor
    ,如下所示:

    - (void)viewDidLoad
    {
        NSClickGestureRecognizer *doubleClickGesture = 
                [NSClickGestureRecognizer alloc] initWithTarget:self
                                                         action:@selector(onDoubleClick:)];
       [doubleClickGesture setNumberOfClicksRequired:2];
       // this should be the default, but without setting it, single clicks were delayed until the double click timed-out
       [doubleClickGesture setDelaysPrimaryMouseButtonEvents:FALSE];
       [self.view addGestureRecognizer:doubleClickGesture];
    }
    
    - (void)onDoubleClick:(NSGestureRecognizer *)sender
    {
        // by sending the action to nil, it is passed through the first responder chain
        // to the first object that implements collectionItemViewDoubleClick:
        [NSApp sendAction:@selector(collectionItemViewDoubleClick:) to:nil from:self];
    }
    

    我确实将
    IconViewBox
    的委托连接到了
    IconCollectionViewItem
    ,尽管它不是文件的所有者,因为我正试图将它添加到我自己的项目中,该项目的设置与他们的示例不同。