我正在尝试使用NSBrowser控件在快速的4 Macos应用程序中进行拖放操作。我目前拥有的代码如下:
// Drag and Drop
func browser(_ browser: NSBrowser,
canDragRowsWith rowIndexes: IndexSet,
inColumn column: Int,
with event: NSEvent) -> Bool {
if column != 0 {
quizBrowser.canDragRows(with: rowIndexes, inColumn: column, with: event)
return true
}
return false
}
func browser(_ browser: NSBrowser,
writeRowsWith rowIndexes: IndexSet,
inColumn column: Int,
to pasteboard: NSPasteboard) -> Bool {
if column != 0 {
let row: Int = rowIndexes.last!
let item = quizBrowser.item(atRow: row, inColumn: column) as? BrowserItem
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: self)
pasteboard.setString(item!.name, forType: NSPasteboard.PasteboardType.string)
return true
}
return false
}
func browser(_ browser: NSBrowser,
validateDrop info: NSDraggingInfo,
proposedRow row: UnsafeMutablePointer<Int>,
column: UnsafeMutablePointer<Int>,
dropOperation: UnsafeMutablePointer<NSBrowser.DropOperation>) -> NSDragOperation {
return NSDragOperation.move
}
func browser(_ browser: NSBrowser,
acceptDrop info: NSDraggingInfo,
atRow row: Int,
column: Int,
dropOperation: NSBrowser.DropOperation) -> Bool {
let pboard = info.draggingPasteboard
let rowData = pboard().data(forType: NSPasteboard.PasteboardType.string)
return true
}
我看到的是前两个函数被称为[canDragRowsWith和writeRowswith],并且setString命令返回true,因此看起来值已保存到粘贴板。
但是其他两个函数没有被调用。因此,虽然我可以看到NSBrowser中的行被拖动,但似乎该控件未注册为接受拖放。我也已经在viewDidLoad函数中添加了这个
quizBrowser.setDraggingSourceOperationMask(NSDragOperation.generic, forLocal: true)
有人知道我在想什么吗?有没有人愿意分享他们使用NSBrowser进行拖放的迅速3/4示例?
谢谢
答案 0 :(得分:0)
在视图可以进行拖动操作之前,您需要注册通过调用可以接受的数据类型
func registerForDraggedTypes(_ newTypes: [NSPasteboard.PasteboardType])