【问题标题】:Drag and drop in an NSView在 NSView 中拖放
【发布时间】:2012-10-31 20:51:52
【问题描述】:

我正在 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

在界面生成器中,我将subView(类设置为testViewDrag)添加到主窗口。在日志中,我可以看到 initWithFrame 日志,但是当我拖动时,日志中没有显示任何内容。

我错过了什么?

【问题讨论】:

    标签: macos drag-and-drop nsdragginginfo


    【解决方案1】:

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

    尝试用这个替换它:

    [self registerForDraggedTypes:[NSArray arrayWithObjects:
            NSColorPboardType, NSFilenamesPboardType, nil]];
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢你的工作就像一个魅力,如果你现在可以告诉我如何获取拖动文件的路径:)
    • 例如 - (BOOL)performDragOperation:(id )sender { NSArray * thefiles = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType]; // 在这里处理你想要的任何东西 }
    • 非常感谢您的帮助,这是我申请中的最后一点,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多