Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Click in NSCollectionView

I'm trying to get my program to recognize a double click with an NSCollectionView. I've tried following this guide: http://www.springenwerk.com/2009/12/double-click-and-nscollectionview.html but when I do it, nothing happens because the delegate in IconViewBox is null:

The h file:

@interface IconViewBox : NSBox
{
    IBOutlet id delegate;
}
@end

The m file:

@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];
        }
    }
}

The second NSLog never gets printed because delegate is null. I've connected everything in my nib files and followed the instructions. Does anyone know why or an alternate why to do this?

like image 341
user1998511 Avatar asked Jan 29 '13 01:01

user1998511


2 Answers

You can capture multiple-clicks within your collection view item by subclassing the collection item's view.

  1. Subclass NSView and add a mouseDown: method to detect multiple-clicks
  2. Change the NSCollectionItem's view in the nib from NSView to MyCollectionView
  3. Implement collectionItemViewDoubleClick: in the associated NSWindowController

This works by having the NSView subclass detect the double-click and it pass up the responder chain. The first object in the responder chain to implement collectionItemViewDoubleClick: is called.

Typically, you should implement collectionItemViewDoubleClick: in the associated NSWindowController, but it can be in any object within the responder chain.

@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
like image 109
Graham Miln Avatar answered Oct 05 '22 05:10

Graham Miln


Another option is to override the NSCollectionViewItem and add an NSClickGestureRecognizer like such:

- (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];
}
like image 33
InfalibleCoinage Avatar answered Oct 05 '22 05:10

InfalibleCoinage