【问题标题】:Drag-and-drop to a non-view拖放到非视图
【发布时间】:2011-10-03 19:57:18
【问题描述】:

是否有人对如何将文件拖动到小目标提出建议。我真正需要的是文件元数据。我不需要显示文件本身,只需要显示它的内容(这些是更像目录的自定义文件)。我已经看到了拖动到 NSView 的示例,但我认为我需要的是在我的 NSObject 类中拖动到一个简单的小型 textView 的示例。然后我需要获取文件内容以便解析它。

Cocoa 是否要求所有拖放都通过视图完成? 非常感谢任何帮助

所以补充一下我之前发布的内容; 我按照http://juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/072-NSView-drag-and-drop.pl 的示例进行拖放图像。它适用于浏览器和查找图像的 Finder,但不适用于其他文件类型。

附加于 2011 年 10 月 4 日 如上所述,我按照上面链接中的拖放示例来删除基于 TIFF 的图像。它可以正常工作,可以从 Web 或 Finder 将图像拖到自定义视图中。我不明白的是我需要做什么才能让它适用于简单的文本文件,更重要的是,自定义文件。我已经阅读了 Mac Dev 网站上的拖放信息,但仍然不够了解,无法进行必要的修改。

这是我的代码:

//myNSView.h
#import <Cocoa/Cocoa.h>

@interface MyNSView : NSView
{
    NSImage *nsImageObj;

}
@property(assign) NSImage *nsImageObj;

-(IBAction)reset:(id)sender;

@end



//myNSV.m


#import "MyNSView.h"

@implementation MyNSView
@synthesize nsImageObj;

- (id)initWithFrame:(NSRect)frame
{
    if(!(self = [super initWithFrame:frame]))
    {
        NSLog(@"Error: MyNSView initWithFrame");
        return self;
    }//end if
    self.nsImageObj = nil;

    [self registerForDraggedTypes:[NSArray arrayWithObjects: NSTIFFPboardType, NSFilenamesPboardType, nil]];
    return self;
}//end initWithFrame

- (void)drawRect:(NSRect)dirtyRect
{
    if(self.nsImageObj == nil){
        [[NSColor blackColor]set];
        NSRectFill(dirtyRect);
    }//end if

    NSRect zOurBounds = [self bounds];
    [super drawRect:dirtyRect];
    [self.nsImageObj compositeToPoint:(zOurBounds.origin) operation:NSCompositeSourceOver];
}
-(IBAction)reset:(id)sender{
    NSLog(@"reset Button Pressed");
    nsImageObj = nil;
   NSLog(@"check Image %@", self.nsImageObj);
    [[NSColor blackColor]set];
    [self setNeedsDisplay:YES];
}

-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender{

    //is the sender looking for NSDragOperationGeneric
    if((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
        return NSDragOperationGeneric;
    else
        return NSDragOperationNone;

}//end draggingEntered


-(BOOL) prepareForDragOperation:(id<NSDraggingInfo>)sender{
    return YES;
}// end prepareForDragOperation

-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
   //initialize pasteboard
    NSPasteboard *zPasteboard = [sender draggingPasteboard];
    //initialize image file types, addedd some extra
    NSArray *zImageTypesArray =[NSArray arrayWithObjects:NSPasteboardTypeTIFF,NSFilenamesPboardType, nil];

    NSString *zDesiredType = [zPasteboard availableTypeFromArray: zImageTypesArray];

            if([zDesiredType isEqualToString:NSPasteboardTypeTIFF]){

                NSData *zPasteboardData = [zPasteboard dataForType:zDesiredType];

                //make sure we have data
                if(zPasteboardData == nil){
                    NSLog(@"Error: MyNsView zPasteBoardData == nil");
                    return NO;
                }//end if nil

                self.nsImageObj = [[NSImage alloc] initWithData: zPasteboardData];
                [self setNeedsDisplay:YES];

                return YES;

            }//end outer if
            //if desired types is a string of filenames
    if([zDesiredType isEqualToString:NSFilenamesPboardType]){
        NSArray *zFileNamesAry = [zPasteboard propertyListForType:@"NSFilenamesPboardType"];
        NSString *zPath = [zFileNamesAry objectAtIndex:0];

        NSImage *zNewImage = [[NSImage alloc] initWithContentsOfFile:zPath];

                if(zNewImage == nil){

                    NSLog(@"Error: MyNSView performDragOperation zNewImage = nil");
                    return NO;
                }

                //else everything is good in here

        self.nsImageObj = zNewImage;
        [self setNeedsDisplay:YES];
        return YES;

    }//end outer if

    //if we get here than there was an unknown error return no
    NSLog(@"Error Unknown in MYNSView performDragOperation");
    return NO;
}

-(void)concludeDragOperation:(id<NSDraggingInfo>)sender{

    [self setNeedsDisplay:YES];

}

@end

我希望有人能指出我需要学习什么才能弄清楚这一点。也许这是我对粘贴板的困惑,也许是我还不知道的另一个组件。一如既往,我非常感谢您的帮助。

谢谢

【问题讨论】:

  • 如果屏幕上有东西可以执行拖放,那肯定是 NSView 吗?

标签: cocoa drag-and-drop


【解决方案1】:

首先,NSTextView 是 NSView 的子类(远距离)。其次,要接收丢弃事件,是的,你确实需要一个视图。第三,您遇到的问题是拖动粘贴板上的信息类型。

按照Drag and Drop Programming Topics 指南中的说明设置视图并注册 NSFilenamesPboardType。当您获取文件名时,请使用 NSFileManager 或使用 Spotlight 元数据 API 来获取您需要的有关文件的任何信息。

【讨论】:

  • 您能否推荐一个链接来显示为各种文件创建拖放区的步骤?我在网上看过一些关于拖放图像的教程。我做了一个
  • 我给你的那个解释了这一点。您尝试了哪些方法,哪些方法不起作用?
  • 当我尝试拖放一个文本文件时,什么也没有发生。如果 if([zDesiredType isEqualToString:NSPasteboardTypeTIFF]) 那么它可以工作,但如果粘贴板不包含该字符串,则它为空。据我了解,它应该有一个文件名字符串数组,其中包含 txt 文件名(NSFilenamesPboardType)。
  • @Miek:黑暗中的狂野刺伤:您忘记注册拖拽视图。如果您提供有关问题的更多信息,包括所有与拖动相关的代码,我们只能提供更多信息或更相关的解决方案。 (请将其编辑到您的问题中 - 它不适合评论。)
  • 我附加了我的帖子,所以它是完整的。请再看看。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-19
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
相关资源
最近更新 更多