2

I have a view which contains a few subviews:

mainView
    subViewA
    subViewB
    SubViewC

mainView is an NSView constructed from a nib and is controlled with an NSViewController subclass. The subviews are standard views such as NSTextField and NSImageView and are configured to be non-editable. I want mainView to receive rightMouseDown: even when the event is triggered in one of the subviews.

The default implementation of rightMouseDown: in NSResponder passes the event to the next responder, but NSView changes the default behaviour and does not pass it to the next responder.

I could subclass all of the subviews but this doesn't seem like a very elegant or maintainable solution.

How can I get the subviews to pass rightMouseDown: messages to the next responder without subclassing all of the subviews?

Benedict Cohen
  • 11,912
  • 7
  • 55
  • 67

3 Answers3

1

This is usually accomplished by overriding the superviews's -hitTest: method?

kperryua
  • 10,524
  • 1
  • 38
  • 24
0

Override NSApplication's - (void)sendEvent:

- (void)sendEvent:(NSEvent *)event {
  if([event type]== NSRightMouseDown)
    redirect to wherever
  else
    [super sendEvent:event];
}
hooleyhoop
  • 9,128
  • 5
  • 37
  • 58
0

My solution is to add a subview to the mainView which I've called clickCatcher. clickCatcher is transparent and equal in size to mainView and is added so that it's the top most view:

[self addSubview:clickCatcher positioned:NSWindowAbove relativeTo:nil];

The XXXmouseDown: methods of clickCatcher call superview.

Benedict Cohen
  • 11,912
  • 7
  • 55
  • 67