8

我试图让我的程序识别带有 NSCollectionView 的双击。我已经尝试遵循本指南:http ://www.springenwerk.com/2009/12/double-click-and-nscollectionview.html但是当我这样做时,没有任何反应,因为 IconViewBox 中的代表为空:

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

4

3 回答 3

7

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

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

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

通常,您应该collectionItemViewDoubleClick:在 associated 中实现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
于 2013-09-19T07:37:34.137 回答
2

另一种选择是覆盖NSCollectionViewItem并添加NSClickGestureRecognizer这样的:

- (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];
}
于 2019-03-08T01:14:12.450 回答
0

尽管您说了些什么,但您需要确保您遵循了教程中的第四步:

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

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

于 2013-01-29T04:10:19.120 回答