NSDraggingDestination 协议中必须使用哪些方法?
What methods are obligatory in the NSDraggingDestination protocol?
In the Docs of the protocol NSDraggingDestination
,它表示:
A set of methods that the destination object (or recipient) of a
dragged image must implement.
接着是九种方法。但我只实现了这九种方法中的三种(在我的 NSView
中):draggingEntered:
、prepareForDragOperation:
和 performDragOperation:
.
它编译和运行时没有警告或崩溃。文档没有说有些方法是必须的,有些方法是可选的,那它是怎么工作的呢?
#import "Common.h"
@interface StageView : NSView <NSDraggingDestination>
@end
#import "StageView.h"
@implementation StageView
-(void)awakeFromNib {
// we want pasteboard to hold a single URL (See Drag and Drop Programming Topics)
NSLog(@"--registerForDraggedTypes");
[self registerForDraggedTypes:@[NSURLPboardType]];
}
#pragma mark - DragAndDrop
-(NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
NSLog(@"--draggingEntered");
return NSDragOperationCopy;
}
-(BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
NSLog(@"--prepareForDragOperation");
//check to see if we can accept the data
return YES;
}
// method that should handle the drop data
-(BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
NSLog(@"--performDragOperation");
NSInteger numFiles = sender.numberOfValidItemsForDrop;
CGPoint loc = sender.draggingLocation;
NSURL *fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]];
NSString *ext = [fileURL pathExtension];
if ([ext isEqualToString:@"mov"] && numFiles == 1) {
[self handleVideo:loc url:fileURL];
return YES;
}
return NO;
}
#pragma mark - Handle Video
-(void)handleVideo:(CGPoint)loc url:(NSURL *)fileURL {
NSLog(@"--handleVideo");
// ...
}
@end
如果您查看实现,您会发现,事实上,所有方法 都是可选的:
public protocol NSDraggingDestination : NSObjectProtocol {
optional public func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation
optional public func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation
optional public func draggingExited(_ sender: NSDraggingInfo?)
optional public func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool
optional public func performDragOperation(_ sender: NSDraggingInfo) -> Bool
optional public func concludeDragOperation(_ sender: NSDraggingInfo?)
optional public func draggingEnded(_ sender: NSDraggingInfo)
optional public func wantsPeriodicDraggingUpdates() -> Bool
optional public func updateDraggingItemsForDrag(_ sender: NSDraggingInfo?)
}
Apple 提供的文档的措辞具有误导性,您应该将其解释为"you must implement them if you want to handle those actions"。
In the Docs of the protocol NSDraggingDestination
,它表示:
A set of methods that the destination object (or recipient) of a dragged image must implement.
接着是九种方法。但我只实现了这九种方法中的三种(在我的 NSView
中):draggingEntered:
、prepareForDragOperation:
和 performDragOperation:
.
它编译和运行时没有警告或崩溃。文档没有说有些方法是必须的,有些方法是可选的,那它是怎么工作的呢?
#import "Common.h"
@interface StageView : NSView <NSDraggingDestination>
@end
#import "StageView.h"
@implementation StageView
-(void)awakeFromNib {
// we want pasteboard to hold a single URL (See Drag and Drop Programming Topics)
NSLog(@"--registerForDraggedTypes");
[self registerForDraggedTypes:@[NSURLPboardType]];
}
#pragma mark - DragAndDrop
-(NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
NSLog(@"--draggingEntered");
return NSDragOperationCopy;
}
-(BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
NSLog(@"--prepareForDragOperation");
//check to see if we can accept the data
return YES;
}
// method that should handle the drop data
-(BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
NSLog(@"--performDragOperation");
NSInteger numFiles = sender.numberOfValidItemsForDrop;
CGPoint loc = sender.draggingLocation;
NSURL *fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]];
NSString *ext = [fileURL pathExtension];
if ([ext isEqualToString:@"mov"] && numFiles == 1) {
[self handleVideo:loc url:fileURL];
return YES;
}
return NO;
}
#pragma mark - Handle Video
-(void)handleVideo:(CGPoint)loc url:(NSURL *)fileURL {
NSLog(@"--handleVideo");
// ...
}
@end
如果您查看实现,您会发现,事实上,所有方法 都是可选的:
public protocol NSDraggingDestination : NSObjectProtocol {
optional public func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation
optional public func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation
optional public func draggingExited(_ sender: NSDraggingInfo?)
optional public func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool
optional public func performDragOperation(_ sender: NSDraggingInfo) -> Bool
optional public func concludeDragOperation(_ sender: NSDraggingInfo?)
optional public func draggingEnded(_ sender: NSDraggingInfo)
optional public func wantsPeriodicDraggingUpdates() -> Bool
optional public func updateDraggingItemsForDrag(_ sender: NSDraggingInfo?)
}
Apple 提供的文档的措辞具有误导性,您应该将其解释为"you must implement them if you want to handle those actions"。