NSWindow支持Drag-and-Drop

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:10   661   0

在NSView中,通过registerForDraggedTypes:来支持Drag-and-Drop。


[self registerForDraggedTypes:@[NSColorPboardType, NSFilenamesPboardType]];


对于NSWindow,通过awakeFromNib来支持Drag-and-Drop




@interface ONMainWindow () <NSDraggingDestination>


@end


@implementation ONMainWindow


- (void)awakeFromNib {
[self registerForDraggedTypes:@[NSFilenamesPboardType]];
}


#pragma mark - NSDraggingDestination
// 拖放显示图标
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
return NSDragOperationCopy;
}


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


// 拖放操作
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
NSPasteboard *pboard = [sender draggingPasteboard];
NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];

if (1 == filenames.count) {
if ([[NSApp delegate] respondsToSelector:@selector(application:openFile:)]) {
return [[NSApp delegate] application:NSApp openFile:[filenames lastObject]];
}
}
return NO;
}


我们在MainWindow.xib中,把Window设置为ONMainWindow。

然后,拖放文件到Windows窗口上,就可以再performDragOperation:中收到我们拖放的文件名或文件路径。

[0] __NSCFString * @"/Volumes/Info/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleOSX10.9.CoreReference.docset/Contents/Resources/Documents/samplecode/CocoaDragAndDrop/Listings" 0x0000608000185480



分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP