Top-level contextual menu for view hierarchy
Top-level contextual menu for view hierarchy
- Subject: Top-level contextual menu for view hierarchy
- From: Matt Reagan <email@hidden>
- Date: Mon, 21 Sep 2015 13:22:43 -0700
Hi cocoa-dev,
I have a custom UI component that wraps a number of subviews (mostly standard Cocoa views: NSTextField, NSImageView, etc.).
I'm attempting to provide a contextual menu for the entire top-level view / control in this case, so that regardless of which particular subview is right/control-clicked, the same menu can be shown. NSView by default does not call into super for -menuForEvent AFAICS, it just returns nil. The problem is easy to reproduce in a test app by adding e.g. an NSImageView to a custom container view subclass. The container view can provide a menu for -menuForEvent, which shows if the user control-clicks anywhere outside the child NSImageView. However a control click on the NSImageView shows nothing.
(NOTE: right-click works in the above scenario, but a control-click does not.)
My question: is there a simple Apple-prescribed approach for providing a single top-level contextual menu for a view *and* its subview tree for both right/control-click? I've found I can get this working with hacky approaches like traversing the view hierarchy and calling -setMenu on every subview, or overriding -menuForEvent on every single subview class, but this seems clunky and unnecessary. I also considered trying to solve this by leveraging -hitTest, but again, that seems like it shouldn't be required.
Does anyone have any suggestions?
Thanks,
-matt
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden