3
votes

I would like be able to drag (any) file to my view-based NSTableView, so in the delegate I have this setup:

class MyViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource, NSDraggingDestination
{
    @IBOutlet var tableView: NSTableView! // connected in storyboard.

    override func viewDidLoad()
    {
        super.viewDidLoad()
        tableView.registerForDraggedTypes([NSFilenamesPboardType])
        // …
    }

    func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation
    {
        println("Drag entered.")
        return .Copy
    }

    func prepareForDragOperation(sender: NSDraggingInfo) -> Bool
    {
        return true
    }

    func draggingUpdated(sender: NSDraggingInfo) -> NSDragOperation
    {
        return .Copy
    }
    // ...
}

But my program just refuses to react to a drag-n-drop. When I drag a file from Finder to it and release, the file icon just flies back to Finder. Am I missing something in my code?

UPDATE: I added this

func performDragOperation(sender: NSDraggingInfo) -> Bool
{
    return true
}

but it still doesn’t work. Should I implement this in my view instead of the delegate? The document says “Either a window object or its delegate may implement these methods;”

2
Have a look at this example.Eric Aya

2 Answers

1
votes

Self-answer:

I stumbled upon this question, and realized that there are methods that need implement in the datasource sector also; namely these. And the drag-n-drop works now.

0
votes

If you look at the sample code Apple puts in their "Receiving Drag Operations" documentation, the last function they put there is an implementation of:

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender

You need to implement that and return "YES" to indicate the drag succeeded.