I have an NSView which covers its parent window's content view. This view has a click event handler which removes it from the content view. Inside this view, I have another view. When I drag the mouse in this inner view, the mouse events are applied not only to the view in the front, but also to the views behind. Additionally, the cursors from the views behind are showing up as well. This is the same problem occurring here: NSView overlay passes mouse events to underlying subviews? but the answer there won't work for my project because I can't open another window.
-
Please post the event-handling code for your top-most view. – Rob Keniger Jul 06 '11 at 02:28
2 Answers
Without seeing your event-handling code it's difficult to know what's happening, but I suspect you might be calling super
's implementation of the various event-handling methods in your implementations.
NSView
is a subclass of NSResponder
, so by default un-handled events are passed up the responder chain. The superview of a view is the next object in the responder chain, so if you call, for example, [super mouseDown:event]
in your implementation of ‑mouseDown:
, the event will be passed to the superview.
The fix is to ensure you don't call super
's implementation in your event handlers.
This is incorrect:
- (void)mouseDown:(NSEvent*)anEvent
{
//do something
[super mouseDown:event];
}
This is correct:
- (void)mouseDown:(NSEvent*)anEvent
{
//do something
}

- 45,830
- 6
- 101
- 134
-
1This is what my issue was except that I needed to implement empty methods for mouse dragged, cursor update, and mouse up, because I already had an implementation for mouse down. Which event, though, handles changing the cursor? – Maz Jul 06 '11 at 14:01
-
Just use `-mouseEntered:` and `-mouseExited:` and set the cursor in those methods. – Rob Keniger Jul 06 '11 at 22:51
-
1
-
This will only disable clicks. If you have a NSTextField in the view below with first responder set, the mouse cursor still seems to change to a beam. – strangetimes Mar 25 '21 at 15:30
Rob's answer and Maz's comment on that answer solve this issue, but just to make it absolutely explicit. In order to prevent a NSView from bleeding it's mouse events to the parent, one must implement the empty methods.
// NSResponder =========================================
- (void) mouseDown:(NSEvent*)event {}
- (void) mouseDragged:(NSEvent*)event {}
- (void) mouseUp:(NSEvent*)event {}

- 4,715
- 5
- 28
- 41