拖放到非视图
有人对如何将文件拖动到小目标有建议吗?我真正需要的是文件元数据。我不需要显示文件本身,只显示它的内容(这些是更像目录的自定义文件)。我已经看到了拖动到 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
我希望有人能指出我需要学习什么才能弄清楚这一点。也许这是我对粘贴板的困惑,也许这是我还不知道的另一个组件。一如既往,我非常感谢您的帮助。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,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.