【问题标题】:Double Click in NSCollectionView在 NSCollectionView 中双击
【发布时间】:2020-05-28 06:37:01
【问题描述】:

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

【问题讨论】:

    标签: cocoa nscollectionview


    【解决方案1】:

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

    1. 子类NSView 并添加mouseDown: 方法来检测多次点击
    2. 将 NSCollectionItem 在 nib 中的视图从 NSView 更改为 MyCollectionView
    3. 在关联的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
    

    【讨论】:

      【解决方案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];
      }
      

      【讨论】:

        【解决方案3】:

        尽管你说了这么多,但你需要确保你遵循了教程中的第四步:

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

        只要您确实按照其余步骤操作,就可以了。

        【讨论】:

        • 我确实将IconViewBox 的代表连接到IconCollectionViewItem,尽管它不是文件的所有者,因为我试图将它添加到我自己的项目中,该项目的设置与他们的示例不同有。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多