有沒有人有關於如何將文件拖動到小目標的建議。我真正需要的是文件元數據。我不需要顯示文件本身,只是它的內容(這些是更像目錄的自定義文件)。我已經看到了拖到NSView的例子,但我想我需要的是拖到我的NSObject類中的一個簡單的小textView的例子。然後我需要獲取文件內容,以便我可以解析它。拖放到非視圖
可可要求所有的拖放操作都是通過視圖完成嗎? 任何幫助非常感謝
因此,以增加我之前發佈的內容; 我遵循http://juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/072-NSView-drag-and-drop.pl的示例來拖放圖像。它適用於瀏覽器和Finder圖像,但不適用於其他文件類型。
追加2011年10月4日 如上所述,我遵循上面鏈接中的拖放示例來刪除基於TIFF的圖像。它可以像從網上或從Finder中將圖像拖放到自定義視圖中那樣工作。我不明白的是我需要做些什麼來使它適用於簡單的文本文件,甚至更重要的是定製文件。我已經閱讀了Mac Dev 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? – iain