gpt4 book ai didi

objective-c - 拖放——只接受文件夹

转载 作者:太空狗 更新时间:2023-10-30 03:43:37 25 4
gpt4 key购买 nike

我正在编写一个需要接受文件夹删除的自定义 View 。条件是:只接受目录,因此当用户拖动文件时什么都不会发生。

我已将我的 View 注册到:

[self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];

并且已经实现了基本的拖动协议(protocol)方法。出于测试目的:

- (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; }

因此它几乎可以正常工作:在 View 上拖动时光标会得到一个加号。但是,如果该项目是常规文件,我想避免这种情况。

一旦获得拖动路径,我可能需要使用 NSFileManager(尽管我想知道是否有更简单的方法)来执行此操作,但问题是在哪里。我试图在返回 NSDragOperationNonedraggingEntered: 方法中包含测试,但没有成功。我正在关注 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;
}

我应该在哪里实现这个测试,以便在拖动文件时光标保持不变?

最佳答案

您应该在 -draggingEntered: 中实现测试,如果粘贴板包含文件,则返回 NSDragOperationNone

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

目前,您总是在 ‑draggingUpdated: 中返回 NSDragOperationCopy,而不测试文件类型,这意味着一旦鼠标移动到您的拖动目标内,无论您在 -draggingEntered: 中做什么,光标都会变为复制光标。

请注意,实现 ‑draggingUpdated: 是可选的。如果您没有实现具有多个可能的拖动目的地的复杂 View ,那么您不需要实现它,只需实现 ‑draggingEntered:‑draggingExited:

关于objective-c - 拖放——只接受文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6208334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com