Mike2012
Mike2012

Reputation: 7725

Catching mouseDown events of subviews. (Cocoa osx)

I have a series of nested views and I need to catch the mouseDown event ant do the same thing when any of these views are selected. Is there a way to tell a superview to handle events for its subviews? Is the best way to handle this to put a transparent view on top of all my other views and have this view handle the events?

Upvotes: 4

Views: 3260

Answers (3)

Nick Moore
Nick Moore

Reputation: 15857

This version works for converting a point expressed in screen coordinates:

NSPointInRect([view convertPoint:[[view window] convertScreenToBase:point] fromView:nil], [view bounds]);

Upvotes: 0

Flenser
Flenser

Reputation: 21

In the superview, you can override hitTest to return the superview if the point is in the superview's rectangle. That will prevent the mouse event from going to any of the subviews.

- (NSView *)hitTest:(NSPoint)aPoint
{
    return NSPointInRect(aPoint, self.frame) ? self : nil;
}

Note that aPoint is in the superview's "frame" coordinate system, not its bounds.

Upvotes: 2

Chris Johnsen
Chris Johnsen

Reputation: 225037

Do your subviews define their own mouseDown:?

If they do not already define their own -[… mouseDown:(id)event], then they should already be passing their events up the responder chain, which should get to your superview.

Otherwise, (in addition to whatever other handling they need to do) they need to decide which events the superview should also receive and call [super mouseDown:event] for those events.

Upvotes: 2

Related Questions