[Cocoa] Finding the destination NSView for a drag drop

Hi,

I have an NSGridView that contains a bunch of custom NSBox's. I want to implement drag drop for these boxes, where each box can be dragged into another box and they swap positions. The declaration of the custom box is like so:

class CustomBox:NSBox, NSDraggingSource

{

override func mouseDown(with event: NSEvent) {

        

        let pasteboardItem = NSPasteboardItem()

        pasteboardItem.setString(self.title, forType:.string)

        let draggingItem = NSDraggingItem(pasteboardWriter: pasteboardItem)

        draggingItem.setDraggingFrame(self.bounds, contents:self)

        beginDraggingSession(with: [draggingItem], event: event, source: self)

    }

    

    func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation {

        switch(context) {

        case .withinApplication  : return .generic

        default : return NSDragOperation()

        }

    }

override func draggingExited(_ sender: NSDraggingInfo?) {

// Get the Destination box here and call DoSwap() to swap the boxes
}

}

From the NSDraggingInfo in draggingExited, I was not getting the destination box. However using

let cell = self.window?.contentView?.hitTest(pSender!.draggingLocation)

returns the reference to the Gridview rather than the NSBox (same scenario happens when using hitTest on the Gridview object after using convert to convert the coordinates).

Is there a way to get the reference to the destination NSBox where the drop has happened?

Thanks!

[Cocoa] Finding the destination NSView for a drag drop
 
 
Q