简体   繁体   English

NSBrowser拖放快速

[英]NSBrowser drag and drop swift

I am trying to get drag and drop working in a swift 4 Macos application with a NSBrowser control. 我正在尝试使用NSBrowser控件在快速的4 Macos应用程序中进行拖放操作。 The code I have at the moment looks like this: 我目前拥有的代码如下所示:

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

What I am seeing is that the first two functions are being called [canDragRowsWith and writeRowswith], and the setString command is returning true, so it looks as though the value has been saved to the pasteboard. 我看到的是前两个函数被称为[canDragRowsWith和writeRowswith],而setString命令返回的是true,因此看起来该值已保存到粘贴板上。

But the other two functions are not being called. 但是其他两个函数没有被调用。 So while I can see the row in the NSBrowser being dragged, it looks as though the control isn't registered to accept the drop. 因此,虽然我可以看到NSBrowser中的行被拖动,但似乎该控件未注册为接受拖放。 I have also added this in the viewDidLoad function 我也在viewDidLoad函数中添加了它

quizBrowser.setDraggingSourceOperationMask(NSDragOperation.generic, forLocal: true)

Does anyone have an idea what I am missing? 有人知道我在想什么吗? Does anyone have a swift 3/4 example of drag and drop using an NSBrowser that they are will to share? 是否有人会共享一个使用NSBrowser进行拖放的快速3/4示例?

Thanks 谢谢

在视图可以进行拖动操作之前,您需要通过调用来注册其可以接受的数据类型

func registerForDraggedTypes(_ newTypes: [NSPasteboard.PasteboardType])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM