在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設(shè)置為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
新聞熱點
疑難解答