NSImageView Double-Click Action

I have several NSImageViews in my Mac app where the user can drag and drop objects like .png or .pdf to save them to custom general defaults that work fine.

Now I would like to set the action when the user double clicks on these NSImageViews, but it seems a bit complicated (I had no problems for NSTableView, but "setDoubleAction" is not available for NSImage, and tons of answers (here or with google) regarding actions NSImageView point to creating an NSButton instead of NSImageView, so that doesn't help)

Here is part of my AppDelegate.h:

@interface AppDelegate : NSObject <NSApplicationDelegate>{ (...) @property (assign) IBOutlet NSImageView *iconeStatus; (...) @end 

and here is part of my AppDelegate.m:

 #import "AppDelegate.h" @implementation AppDelegate (...) @synthesize iconeStatus = _iconeStatus; (...) - (void)awakeFromNib { (...) [_iconeStatus setTarget:self]; [_iconeStatus setAction:@selector(doubleClick:)]; (...) } (...) - (void)doubleClick:(id)object { //make sound if that works ... [[NSSound soundNamed:@"Basso"] play]; } 

But that does not work.

Can someone tell me this is the easiest way to do this?

+6
source share
1 answer

You need to subclass NSImageView and add the following method to your subclass:

 - (void)mouseDown:(NSEvent *)theEvent { NSInteger clickCount = [theEvent clickCount]; if (clickCount > 1) { // User at least double clicked in image view } } 
+8
source

All Articles