xcode - Cocoa 中的拖放 (<NSDraggingDestination>) 不起作用

标签 xcode cocoa drag-and-drop nsdragginginfo

我想在我的 Mac 应用程序中实现一些“简单”的拖放操作。 我拖入一个 View 并相应地在我的 nib 文件中输入“MyView”。但是,我在控制台中没有收到任何响应(每当触发任何协议(protocol)方法时,我都会尝试记录消息)

我像这样子类化了 NSView:

@interface MyView : NSView <NSDraggingDestination>{
    NSImageView* imageView;
}

并像这样实现它:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self registerForDraggedTypes:[NSImage imagePasteboardTypes]];

        NSRect rect = NSMakeRect(150, 0, 400, 300);
        imageView = [[NSImageView alloc] initWithFrame:rect];

        [imageView setImageScaling:NSScaleNone];
        [imageView setImage:[NSImage imageNamed:@"test.png"]];
        [self addSubview:imageView];



    }

    return self;
}


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

}

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender{
    return NSDragOperationCopy;

}


- (void)draggingExited:(id <NSDraggingInfo>)sender{
    NSLog(@"exited");

}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender{
    NSLog(@"som");
    return YES;

}

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {

    NSPasteboard *pboard = [sender draggingPasteboard];

    if ( [[pboard types] containsObject:NSURLPboardType] ) {
        NSURL *fileURL = [NSURL URLFromPasteboard:pboard];
        NSLog(@"%@", fileURL);
    }
    return YES;
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender{
    NSLog(@"conclude sth");


}

- (void)draggingEnded:(id <NSDraggingInfo>)sender{
    NSLog(@"ended");


}

- (BOOL)wantsPeriodicDraggingUpdates{
    NSLog(@"wants updates");

}


- (void)updateDraggingItemsForDrag:(id <NSDraggingInfo>)sender NS_AVAILABLE_MAC(10_7){
}

最佳答案

如果有人仍然需要这个,请使用:

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

而不是:

[self registerForDraggedTypes: 
                      [NSImage imagePasteboardTypes]]; // BAD: dropped image runs away, no draggingdestination protocol methods sent...

帮我解决了这个问题。我怀疑这有效,因为我的 XIB 的目标是 osX 10.5。 NSFilenamesPboardType 表示 10.5 及之前的“标准数据”UTI,[NSImage imagePasteboardTypes] 返回 10.6 及之后的标准数据 UTI。

顺便说一句,在 - (BOOL)performDragOperation:(id NSDraggingInfo)sender 中,您可以获取已删除文件的路径:

 NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];

在这里找到解决方案:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DragandDrop/Tasks/acceptingdrags.html#//apple_ref/doc/uid/20000993-BABHHIHC

关于xcode - Cocoa 中的拖放 (<NSDraggingDestination>) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12168059/

相关文章:

objective-c - 如何保持调整大小的窗口的纵横比?

android - 如何避免 Android 按钮拖出屏幕

jquery - 为什么可拖动元素的克隆在 Jquery ui 中的可放置 div 中放置时会恢复?

javascript - 使用 overflow-y 时拖动元素会隐藏

ios - 在哪里保存以编程方式创建的文件,以保护它们免受更新?

iOS Swift 动画 - 将图像移动到新位置

xcode - 安装和使用 Pantomime 框架...找不到 Pantomime/Pantomime.h

ios - NSCalendar 一个月内的最大天数

xcode - 复制并重命名 Xcode 项目及关联文件夹

ios - Xcode 7.0 测试版 :- ibtool failed with exit code 255