在Mac上,将文件拖到我的NSTableVIew?

时间:2015-04-26 02:32:38

标签: macos swift drag-and-drop nstableview

我希望能够将(任意)文件拖到基于视图的NSTableView中,所以在委托中我有这样的设置:

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
    }
    // ...
}

但是我的程序只是拒绝对拖拽做出反应。当我将文件从Finder拖到它并释放时,文件图标就会飞回Finder。我在代码中遗漏了什么吗?

更新:我添加了这个

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

但它仍然无效。我应该在我的视图中而不是委托中实现这个吗?该文档说“窗口对象或其委托可以实现这些方法;”

1 个答案:

答案 0 :(得分:0)

如果你查看Apple放在"Receiving Drag Operations" documentation中的示例代码,他们放在那里的最后一个函数是:

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

您需要实现它并返回“YES”以指示拖动成功。