【问题标题】:How to enable drop something on the Trash in objective-c?如何在objective-c中启用将某些东西放在垃圾箱上?
【发布时间】:2013-01-21 19:31:51
【问题描述】:

我已经实现了从 Finder 拖放到我的应用到 NSTableView,并创建了 link 到文档等。

但是,我想通过将项目从 NSTableView 拖放到垃圾图标上来进行删除操作。我怎样才能正确地做到这一点?如何启用丢弃到垃圾箱?

【问题讨论】:

  • Dock 中的垃圾箱不是应用程序。

标签: objective-c cocoa drag-and-drop recycle-bin


【解决方案1】:

(我已经很长时间没有这样做了,我是从记忆中做的,并看了一下文档。如果这不起作用,请告诉我,我会仔细检查代码.)

draggingSession:sourceOperationMaskForDraggingContext: 中,您应该包含NSDragOperationDelete 作为合法操作之一。然后您将在您的draggingSession:endedAtPoint:operation: 中收到NSDragOperationDelete,以表明该项目已被丢弃在垃圾箱中。

【讨论】:

  • 我在NSTableView - (void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session endedAtPoint:(NSPoint)screenPoint operation:(NSDragOperation)operation 中有方法,但我看到那里我只能读到operation == NSDragOperationDelete,但我无法将其移至垃圾箱。图标还是一样的,应该变暗了。在这个方法- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation 我返回了`NSDragOperationDelete。
  • 我不理解“但我无法将其移至垃圾箱”的评论。如果您收到NSDragOperationDelete,那么您的工作就是从数据中删除元素并告诉表格进行更新。你能更清楚地描述症状吗?
  • draggingSession:sourceOperationMaskForDraggingContext:拖放时不调用此方法。这就是为什么我在方法 draggingSession:endedAtPoint:operation: 在这个方法操作中没有收到任何操作 return me 0 你能帮我吗?
  • 您是否正在为至少 10.7 构建?如果您正在构建 10.6 或更早版本,则需要使用 draggingSourceOperationMaskForLocal:。阅读developer.apple.com/library/mac/documentation/cocoa/reference/…中的概述
【解决方案2】:

使用 dropSessionDidEnd 委托方法。在那里你可以得到放置点的位置,不需要另一个 UICollectionView/UITableView :

func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {

  guard let item = session.items.first?.localObject as? YourObject else {
    return
  }
  let dropLocation = session.location(in: self.view)
  let itemDropedInTrash = TrashImageView.frame.contains(dropLocation)
  if itemDropedInTrash {
    //here update your datasource and reload your collectioView/tableView
    //deleteItemFromDataSource(item: item)
    //collectionView.reloadData()

  }
}

【讨论】:

    猜你喜欢
    • 2011-07-17
    • 2016-10-08
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多