当我将项目从NSCollectionView拖动到Finder时,如何获取拖动目标URL

时间:2013-05-23 02:21:37

标签: objective-c macos cocoa nscollectionview

当我将NSCollectionViewItem拖到Finder时,我正在开发一个将文件复制到Finder的函数,所以我实现了以下委托:

- (BOOL)collectionView:(NSCollectionView *)cv writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard
{

    NSMutableArray *urls = [NSMutableArray array];
    [indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop)
     {
         NSDictionary *dictionary = [[cv content] objectAtIndex:idx];
         [urls addObject:[dictionary objectForKey:KEY_URL]];
     }];

    if ([urls count] > 0)
    {
        [pasteboard clearContents];
        BOOL result = [pasteboard writeObjects:urls];
        return result;
    }
    return NO;
}

或者这样:

- (BOOL)collectionView:(NSCollectionView *)cv writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard
{

    NSMutableArray *urls = [NSMutableArray array];
    [indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop)
     {
         NSDictionary *dictionary = [[cv content] objectAtIndex:idx];
         [urls addObject:[[dictionary objectForKey:KEY_URL] path]];
     }];

    [pasteboard declareTypes:@[NSFilenamesPboardType] owner:self];
    [pasteboard setPropertyList:urls forType:NSFilenamesPboardType];

    return YES;
}

他们都很好。

现在我真的想获得其他功能的拖动目标网址。 有人可以帮我解决一下吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试:

- (BOOL)collectionView:(NSCollectionView *)cv writeItemsAtIndexes:(NSIndexSet *)索引toPasteboard:(NSPasteboard *)粘贴板 {

NSMutableArray *urls = [NSMutableArray array];
[indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop)
 {
     NSDictionary *dictionary = [[cv content] objectAtIndex:idx];
     [urls addObject:[[dictionary objectForKey:KEY_URL] path]];
 }];

[pasteboard clearContents];

return [pasteboard writeObjects:urls];

}