【发布时间】: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