On Mac, drag and drop the file into my NSTableVIew?

I would like to be able to drag and drop (any) file into my view based NSTableView, so the delegate has this setting:

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 respond to drag-n-drop. When I drag a file from Finder onto it and release it, the file icon returns 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 a delegate? The document says: "Any window object or its delegate can implement these methods;"

+3


source to share


2 answers


Self-answer:



I stumbled upon this question and realized that there are techniques that need to be implemented in the data source sector; namely these . And drag-n-drop works now.

+1


source


If you look at the sample code that Apple puts in their documentation Getting Drag and Drop Operations , the last feature they pose is the implementation

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



You need to implement this and return " YES

" to indicate that the drag was successful.

0


source







All Articles