Macos 拖放不在Cocoa中工作
我已经实现了Macos 拖放不在Cocoa中工作,macos,cocoa,drag-and-drop,nscollectionview,Macos,Cocoa,Drag And Drop,Nscollectionview,我已经实现了acceptDrop和validateDrop函数,以及registerForTypes,但是当我拖动一个项目时,它甚至不显示加号 extension Document: NSCollectionViewDelegate { func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndex proposedDropI
acceptDrop
和validateDrop
函数,以及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])
}
扩展文档:NSCollectionViewDelegate{
func collectionView(uCollectionView:NSCollectionView,validateDrop DragginInfo:nsDragginInfo,proposedIndex proposedDropIndex:UnsafemeutablePointer,dropOperation proposedDropOperation:UnsafemeutablePointer)->NSDragOperation{
返回nsdragooperation.copy
}
func collectionView(collectionView:NSCollectionView,acceptDrop DragginInfo:nsDragginInfo,index:Int,dropOperation:NSCollectionViewDropOperation)->Bool{
设pasteboard=DragginInfo.draggingPasteboard()
如果pasteboard.types?包含(NSURLPboardType)=true,则将url=NSURL(from:pasteboard)设为?url{
做{
试试self.addAttachmentAtur(url)
attachmentsList.reloadData()
返回真值
}将let错误捕获为NSError{
self.presentError(错误)
返回错误
}
}
返回错误
}
}
重写func windowController IDLOADNIB(windowController:NSWindowController){
self.attachmentsList.register(ForDragedTypes:[NSURLPboardType])
}
请包含代码以便更容易找到问题您是否拖动
NSURLPboardType
?你连接/设置代理了吗?@Willeke,我拖动了一个普通文件。什么的委托?您是否将集合视图的委托连接或设置到文档?@Willeke,是的,我连接或设置了。