在NSView中拖放 [英] Drag and drop in an NSView

查看:415
本文介绍了在NSView中拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在testView中拖放了一个NSView,但是从来没有调用 drageringEntered:代码:

I'm testig drag and drop in an NSView, but draggingEntered: is never called. code :

#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(class is设置为testViewDrag)到主窗口。
在日志中我可以看到 initWithFrame 日志,但是当我拖曳没有显示在日志中。

in interface builder i add a subView ( class is set to testViewDrag ) to the main window. in log i can see the initWithFrame log but when i drag nothing is shown in the log.

我想要的是什么?

推荐答案

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

"To receive drag operations, you must register the pasteboard types that your window or view will accept by sending the object a registerForDraggedTypes: message, defined in both NSWindow and NSView, and implement several methods from the NSDraggingDestination protocol. During a dragging session, a candidate destination receives NSDraggingDestination messages only if the destination is registered for a pasteboard type that matches the type of the pasteboard data being dragged. The destination receives these messages as an image enters, moves around inside, and then exits or is released within the destination’s boundaries." You can read more about the drag and drop programming topic here. As I see it, your problem lies in the argument you define in your registerForDraggedTypes: method.

尝试用以下替换方法:

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

希望这有帮助!

这篇关于在NSView中拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆