问题描述
我有一个NSCollectionView绑定到一个NSArrayController。我想要拖放工作,所以我创建一个委托并实现方法
- (BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)index withEvent:(NSEvent *)event
- (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id< NSDraggingInfo>)draggingInfo index:(NSInteger)index dropOperation (NSCollectionViewDropOperation)dropOperation
- (NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id< NSDraggingInfo>)draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation
- (NSArray *)collectionView:(NSCollectionView *)collectionView namesOfPromisedFilesDroppedAtDestination :( NSURL *)dropURL forDraggedItemsAtIndexes:(NSIndexSet *)indexes
$ b b我为两个BOOL方法返回YES,对validateDrop:方法使用NSDragOperationMove,对namesOfPromisedFilesDroppedAtDestination:方法使用空数组。我也有一个NSLog语句作为第一行在每个方法,所以我可以看到,当他们被调用。
现在,唯一可以调用的方法是canDragItemsAtIndexes:(其中我返回YES)。我看到它被调用,但任何进一步的拖动只是修改选择。其余的从未被调用。
如果我使NSCollectionView不支持选择,那么甚至不调用该方法。
我确定我错过了一些非常明显的东西,但我不知道它是什么。有谁得到拖放工作与NSCollectionViews和可以散发一些光?
解决方案我想你错过了将粘贴内容写入粘贴板的部分。
您必须执行以下步骤:
- 确定是否可以拖动拖动源
- 如果
是
,请将内容写入粘贴板
- 验证&接受您的放置目标中的项目
写入粘贴板应在
中实现
您还必须注册拖动的类型
一些示例代码:
I have an NSCollectionView bound to an NSArrayController. I want to get drag and drop working, so I create a delegate and implement the methods
-(BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent*)event -(BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id < NSDraggingInfo >)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation -(NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id < NSDraggingInfo >)draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation -(NSArray *)collectionView:(NSCollectionView *)collectionView namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropURL forDraggedItemsAtIndexes:(NSIndexSet *)indexes
I'm returning YES for the two BOOL methods, NSDragOperationMove for the validateDrop: method, and an empty array for the namesOfPromisedFilesDroppedAtDestination: method. I also have an NSLog statement as the first line in each method so I can see when they get called.
Right now, the only method that gets called is canDragItemsAtIndexes: (where I return YES). I see that it gets called, but any further dragging just modifies the selection. The rest never get called.
If I make the NSCollectionView not support selections, then not even that method gets called.
I'm sure I'm missing something super obvious, but I can't figure out what it is. Has anyone gotten drag and drop working with NSCollectionViews and can shed some light?
解决方案I think you miss the part where you write the drag content to the pasteboard.
To support drag and drop you have to perform the following steps:
- Determine if you can drag in your drag source
- If
YES
, write the content to the Pasteboard- Validate & accept the items in your drop target
Writing to the Pasteboard should be implemented in
- collectionView:writeItemsAtIndexes:toPasteboard:
You also have to register your dragged types with
- registerForDraggedTypes:
Some sample code:http://developer.apple.com/library/mac/#samplecode/IconCollection/Introduction/Intro.html
这篇关于NSCollectionView拖放:大多数代理事件未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!