Re: Can NSView and NSImageView respond to Mouse Clicks?

Jack Brindle

NSView inherits from NSResponder. That gives NSView access to mouseDown, mouseUp and a whole host of other important calls.
What this really means is that you can implement
- (void)mouseDown:(NSEvent *)event;
v- (void) mouseUp:(NSEvent *)event;
in your NSView subclass and handle mouse events the way you like, which includes generating IBAction calls to other objects that need to be notified of mouse events in your view.
The event object will contains information about where the cursor was when the mouse down or up even occurred, which you can use to determine exactly where in the view the hit occurred, and even when.
Note that any control embedded in the view will take precedence over the view itself, so that it gets a chance to handle the mouse down first.

After all these years, I still find it really cool the way events and the responder chain works, and gives us so much power to create great user interfaces.


On Sep 20, 2018, at 4:36 AM, Dave <dave@...> wrote:


This is for NSView, I’ve not looked into NSImageView as yet.

I tried adding a Gesture Recognizer which sort of work except the Handler gets calls more than nice when I mouse down.

Basically, I want to detect a Mouse Down on an NSView Subclass and when it occurs display a Pop-Up Menu. Don’t understand why I'm getting multiple calls - see code below.

All the Best

-(instancetype) initWithCoder:(NSCoder*) theDecoder
self = [super initWithCoder:theDecoder];
if (self == nil)
return nil;

NSLog(@"initWithCoder: %@",self.identifier);
[self setupView];

return self;

-(void) setupView
NSPressGestureRecognizer* myGestureRecognizer;

myGestureRecognizer = [[NSPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionPressGesture:)];
myGestureRecognizer.minimumPressDuration = 0.001;
myGestureRecognizer.buttonMask = 1;
myGestureRecognizer.delaysPrimaryMouseButtonEvents = NO;
[self addGestureRecognizer:myGestureRecognizer];

-(void) actionPressGesture:(LTWTestViewCell*) theSender
NSLog(@"actionPressGesture: %@",self.identifier);

On 20 Sep 2018, at 02:03, Alex Zavatone via Groups.Io <zav@...> wrote:

You can add a gesture to it IIRC.
On Sep 19, 2018, at 1:34 PM, Dave <dave@...> wrote:


Can an Custom NSView respond to Mouse Clicks via IBActions?

Can the same be done with NSImageView? I’ve added an image view and connected it to an IBAction method define in my View Controller, but when I click the NSImageView nothing happens…..

Any help greatly appreciated.

All the Best

Join { to automatically receive all group messages.