拖放到NSView中

Red*_*Mak 3 macos drag-and-drop nsdragginginfo

我是在一个NSView中拖放,但从draggingEntered:未被调用过.代码:

#import <Cocoa/Cocoa.h>
@interface testViewDrag : NSView <NSDraggingDestination>
@end

@implementation testViewDrag
- (id)initWithFrame:(NSRect)frame
{
   self = [super initWithFrame:frame];
   if (self)
{
    [self registerForDraggedTypes:[NSImage imagePasteboardTypes]];
    NSLog(@"initWithFrame");
}
return self;
}

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

-(NSDragOperation) draggingUpdated:(id<NSDraggingInfo>)sender
{
  NSLog(@"draggingUpdated");
  return NSDragOperationEvery;
}
@end
Run Code Online (Sandbox Code Playgroud)

在界面构建器中,我将一个subView(类设置为testViewDrag)添加到主窗口.在日志中我可以看到initWithFrame日志,但是当我拖动时,日志中不会显示任何内容.

我错过了什么?

Joh*_*dal 8

"要接收拖动操作,您必须通过向对象发送一个在NSWindow和NSView中定义的registerForDraggedTypes:消息来注册您的窗口或视图将接受的粘贴板类型,并从NSDraggingDestination协议实现多个方法.在拖动会话期间,候选目标只有在目标被注册为与被拖动的粘贴板数据类型匹配的粘贴板类型时才会收到NSDraggingDestination消息.目标在图像进入,内部移动,然后退出或在目标的边界内释放时接收这些消息".您可以在此处阅读有关拖放编程主题的更多信息.在我看来,你的问题在于你在registerForDraggedTypes:方法中定义的参数.

尝试用这个替换它:

[self registerForDraggedTypes:[NSArray arrayWithObjects:
        NSColorPboardType, NSFilenamesPboardType, nil]];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 例如, - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {NSArray*thefiles = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType]; //处理你想要的任何东西} (3认同)