拖放到非视图

发布于 2024-12-07 13:52:30 字数 4245 浏览 1 评论 0 原文

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

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

所以添加到我之前发布的内容中; 我按照 http:// juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/072-NSView-drag-and-drop.pl 用于拖放图像。它对于浏览器和图像查找器都非常有效,但不适用于其他文件类型。

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

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

谢谢

Does anybody have a suggestion on how to go about dragging a file to a small target. What I really need is the files metadata. I don't need to display the file itself, only it's contents(these are custom files that are more like directories). I have seen examples for dragging to an NSView but what I think I need is an example of dragging to a simple, small textView in my NSObject class. Then I need to get the files contents so I can parse it.

Does Cocoa require that all drag and drop be done via Views?
Any Help very much appreciated

So to add to what I posted earlier;
I followed the example at http://juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/072-NSView-drag-and-drop.pl for drag and drop images. It works great for both browsers and the Finder for images, but it will not work for other file types.

appended 10/4/2011
As stated above, I followed the drag and drop example at the link above for dropping TIFF based images. It works as it should for dragging images into a custom view both from the web or from the Finder. What I don't understand is what I need to do to get it to work for simple text files and even more importantly, custom files. I have read the drag and drop information on the Mac Dev site but still don't understand it enough to make the necessary modifications.

Here is my code:

//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

I am hoping some one can point out what I need to learn to get this figured out. Maybe its my confusion with Pasteboards, maybe its another component I'm not even aware of yet. As always, I really appreciate the help.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2024-12-14 13:52:30

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

设置视图并注册 NSFilenamesPboardType,如 拖放编程主题指南。当您获取文件名时,请使用 NSFileManager 或使用 Spotlight 元数据 API 获取您需要的有关文件的任何信息。

First, an NSTextView is a subclass of NSView (distantly). Second, to receive drop events, yes, you really do need a view. Third, what you're having trouble with is the type of information on the dragging pasteboard.

Set up a view and register for NSFilenamesPboardType as described in the Drag and Drop Programming Topics guide. When you get your file names, use NSFileManager or use the Spotlight metadata API to get whatever information about the file that you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文