Consider a view (parent
) with a subview (child
). Both have registered for the dragged type NSFilenamesPboardType
.
parent
is only interested in image files and child
in audio files. If they detect a file of the corresponding type in draggingEntered:
they return NSDragOperationCopy
. If not, they return NSDragOperationNone
.
child
overlaps parent
and when a drag reaches child
, parent
receives a draggingExited:
message, no matter if child
is interested in the drag or not.
How can I make parent
receive the drag if child
does not want it?
If I get this right you should call:
[self.superview yourmethod];
in the childs function.
If the child inherited from the parent you can call:
[super yourmethod];
There are a couple of things you could do here:
As @Thomas mentioned, you could implement logic in the child view to determine whether or not the drag events should be forwarded to the parent view and forward the events when appropriate.
You could unregister the child view from receiving drag messages ([childView unregisterDraggedTypes]
) and then you could implement all of the logic for drags in the parentView. Essentially, the idea would be to calculate the type and position of the drag and whether or not it intersects with the child view. If it does intersect with the child view, apply the drag logic for the child view. Otherwise, apply the drag logic for the parent view. You will probably need to implement draggingUpdated:
to accomplish this.
You can consider make the parent as the only one which register for NSFilenamesPboardType.
And like the UIResponderChain, create a category for each subviews
- (NSView*) dragTest:(id< NSDraggingInfo >)sender;
if the returned value is nil, it means that the subview (and its subivews) doesn't wish to handle the drag event.
Now back to parentview, when draggingEntered: triggered, calculate the location and find out which view it is on, call dragTest: to check if the corresponding subview wish to handle it. Let the returned non-null view to handle the event, otherwise handle it in the parent.
This method will be helpful if you have many level of subviews.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With