拖放不能在Cocoa中工作

时间:2017-04-13 19:14:34

标签: macos cocoa drag-and-drop nscollectionview

我已经实施了acceptDropvalidateDrop功能,以及registerForTypes,但是当我拖动项目时,它甚至都没有显示加号。

extension Document: NSCollectionViewDelegate {

    func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndex proposedDropIndex: UnsafeMutablePointer<Int>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation {
        return NSDragOperation.copy
    }

    func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, index: Int, dropOperation: NSCollectionViewDropOperation) -> Bool {
        let pasteboard = draggingInfo.draggingPasteboard()
        if pasteboard.types?.contains(NSURLPboardType) == true, let url = NSURL(from: pasteboard) as? URL {
            do {
                try self.addAttachmentAtURL(url)
                attachmentsList.reloadData()
                return true
            } catch let error as NSError {
                self.presentError(error)
                return false
            }
        }
        return false
    }
}

override func windowControllerDidLoadNib(_ windowController: NSWindowController) {
    self.attachmentsList.register(forDraggedTypes: [NSURLPboardType])
}

0 个答案:

没有答案