拖放 - 仅接受文件夹

sid*_*yll 6 cocoa drag-and-drop objective-c osx-snow-leopard nsdragginginfo

我正在编写一个需要接受文件夹丢弃的自定义视图.条件是:只接受目录,因此当用户拖动文件时,不会发生任何事情.

我注册了我的观点:

[self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
Run Code Online (Sandbox Code Playgroud)

并且已经实现了基本的拖动协议方法.用于测试目的:

- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{
    NSLog("@Drag Entered");
    return NSDragOperationCopy;
}

- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender
{
    return NSDragOperationCopy;
}

- (void)draggingExited:(id<NSDraggingInfo>)sender
{
    NSLog(@"Dragging Exited");
}

- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender { return YES; }
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender    { return YES; }
Run Code Online (Sandbox Code Playgroud)

所以它的工作原理几乎正确:在视图上拖动时光标会出现加号.但是,如果该项目是常规文件,我想避免这种情况.

我可能需要使用NSFileManager(虽然我想知道是否有更简单的方法)一旦我得到拖动的路径,但问题是在哪里.我试图在draggingEntered:返回NSDragOperationNone的方法中包含测试,但没有成功.我正在关注Apple文档的片段:

{
    NSPasteboard *pboard = [sender draggingPasteboard];

    if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
        NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
        int numberOfFiles = [files count];
        // Perform operation using the list of files
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

我应该在哪里实现此测试,因此如果拖动文件,光标保持不变?

Rob*_*ger 6

如果粘贴板包含文件,则应实施测试-draggingEntered:并返回NSDragOperationNone.

但是,由于您还实现了?draggingUpdated:,因此您还需要将文件夹类型的测试添加到该方法中.

目前你总是NSDragOperationCopy?draggingUpdated:没有测试文件类型的情况下返回,这意味着只要鼠标在拖动目标内移动,无论你做什么,光标都会变为复制光标?draggingEntered:.

请注意,实现?draggingUpdated:是可选的.如果您没有实现具有多个可能的拖动目标的复杂视图,那么您不需要实现它,只是?draggingEntered:?draggingExited:.