Search code examples
objective-cmacosdrag-and-dropnsimageviewnsdragginginfo

How do I detect if a drag operation was cancelled using NSDraggingInfo or similar?


I've created a subclass of NSImageView and implemented the informal protocol for dragging images between other instances of the same class. I am keeping a reference to the image of the view prior to the dragging operation and am able to set it back to said image given certain criteria.

However, I can't seem to detect if the dragging operation was cancelled. I know that the draggingEnded method is called but it is also called when a drag was successful. Any ideas?


Solution

  • I encountered a similar requirement. The fact that you can examine the operation argument passed to the NSDraggingSource protocol method draggedImage:endedAt:operation: to detect drag cancellation is not very well documented.

    In the draggedImage:endedAt:operation: method just add the following check:

    if (operation == NSDragOperationNone)
        return;
    
    // Otherwise perform any drag completion tasks.