18

I have two NSViews on top of each other. One NSView presents a table with rows. On clicking a row another view is shown on top of that view.

Now the problem is when I click on an area on the second view where there is a row on the underneath NSView then it gets clicked. How can I stop that?

Thanks

Leo
  • 1,547
  • 3
  • 24
  • 40

3 Answers3

15

On your top level view, implement mouseDown: in that view and do not call nextResponder or do anything in it.

- (void)mouseDown:(NSEvent *)theEvent{
    //Do nothing to not propagate the click event to descendant views   
}

I have the exact same scenario as the OP and this is working for me as intended. Credit to the answer I found here that led me to this.

zic10
  • 2,310
  • 5
  • 30
  • 55
1

Subclass and implement acceptsFirstMouse in the view, returning YES. Also, set acceptsTouchEvents to YES in that view.

import Cocoa
class UnView: NSView{
    override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
        return false
    }
    override var allowedTouchTypes: NSTouch.TouchTypeMask {
        get { return [] }
        set { }
    }
}
Fattie
  • 27,874
  • 70
  • 431
  • 719
Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
-9

You can enable touch and mouse events in the top view when it is presented:

topView.acceptsTouchEvents = YES;
topView.acceptsFirstMouse = YES;

Edit: the comment is correct that acceptsFirstMouse isn't a property, and must be updated in a subclass.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101