4

我已经对 NSCollectionView 进行了子类化,并且正在尝试从 Finder 接收拖动的文件。我正在接收draggingEntered:并返回一个适当的值,但我从来没有收到prepareForDragOperation:(也没有在此过程中之后的任何方法)。我在这里有什么明显的遗漏吗?

代码:

- (void)awakeFromNib
{
    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSLog(@"entered"); //Happens
    NSPasteboard *pboard;
    NSDragOperation sourceDragMask;

    sourceDragMask = [sender draggingSourceOperationMask];
    pboard = [sender draggingPasteboard];

    if ([[pboard types] containsObject:NSFilenamesPboardType])
    {
        NSLog(@"copy"); //Happens
        return NSDragOperationCopy;
    }

    return NSDragOperationNone;
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    NSLog(@"prepare"); //Never happens
    return YES;
}
4

3 回答 3

6

这已经很晚了,但我发现了问题:

NSCollectionView 默默地提供了一个不兼容的实现:

-(NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender

...而苹果公司没有记录这一点。如果您只是实现该方法以重新调用 draggingEntered 方法,则一切正常,例如:

-(NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender
{
    return [self draggingEntered:sender];
}

(我来到 SO 希望找到这个自定义实现提供了什么“魔法”的解释,因为这也是......未记录的(感谢 Apple!)。我猜它在管理插入点方面做得很聪明集合视图?)。

更新:似乎特殊的魔法在 NSCollectionView 的委托对象内部。出于某种原因,Xcode4 声称我没有委托,但分配它构建并运行正常。在那里查看所有自定义/半文档化的拖放方法。

(或者就像我上面描述的那样,覆盖自定义行为,并实现一些你可以理解的东西)

于 2011-05-02T22:40:31.253 回答
1

您可能想尝试NSCollectionViewDelegate 协议中的这些委托方法

- (NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id <NSDraggingInfo> )draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation;
- (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id <NSDraggingInfo> )draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation;

- (BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event;
- (NSImage *)collectionView:(NSCollectionView *)collectionView draggingImageForItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event offset:(NSPointPointer)dragImageOffset;
- (NSArray *)collectionView:(NSCollectionView *)collectionView namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropURL forDraggedItemsAtIndexes:(NSIndexSet *)indexes;
- (BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard;

尤其是前两种方法。

于 2010-05-03T14:06:00.290 回答
0

我不久前经历过这个。这对我来说似乎违反直觉,但我可以让它工作的唯一方法是将关联的滚动视图设置为放置目标。

于 2010-05-03T06:50:12.913 回答