Reputation: 465
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?
Upvotes: 9
Views: 3934
Reputation: 1
If you are listening for double click, you can also create a subclass of NSView
and override its rightMouseDown
method:
class MyView: NSView {
override func rightMouseDown(with event: NSEvent) {
print("Right mouse down")
}
}
Upvotes: 0
Reputation: 608
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];
}
Upvotes: 4
Reputation: 2792
You can capture multiple-clicks within your collection view item by subclassing the collection item's view.
NSView
and add a mouseDown:
method to detect multiple-clicksNSView
to MyCollectionView
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
Upvotes: 9
Reputation: 6036
What you said notwithstanding, you need to be sure you followed step four in the tutorial:
4. Open IconViewPrototype.xib in IB and connect the View's delegate outlet with "File's Owner":
That should do ya, provided you did follow the rest of the steps.
Upvotes: 0