Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Click through to a NSView behind another NSView

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

like image 429
Leo Avatar asked Jul 13 '12 18:07

Leo


2 Answers

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.

like image 147
zic10 Avatar answered Oct 13 '22 20:10

zic10


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 { }
    }
}
like image 1
Peter DeWeese Avatar answered Oct 13 '22 20:10

Peter DeWeese