【发布时间】:2015-07-04 13:34:00
【问题描述】:
我希望能够将(任何)文件拖到基于视图的 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
}
但它仍然不起作用。我应该在我的视图中而不是委托中实现它吗?文档说“窗口对象或其委托都可以实现这些方法;”
【问题讨论】:
-
看看this example。
标签: macos swift drag-and-drop nstableview