Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 拖放到非视图_Cocoa_Drag And Drop - Fatal编程技术网

Cocoa 拖放到非视图

Cocoa 拖放到非视图,cocoa,drag-and-drop,Cocoa,Drag And Drop,有人对如何将文件拖到一个小目标有什么建议吗。我真正需要的是文件元数据。我不需要显示文件本身,只需要显示其内容(这些是更像目录的自定义文件)。我已经看到了拖动到NSView的示例,但我认为我需要的是在我的NSObject类中拖动到一个简单、小的textView的示例。然后我需要得到文件的内容,这样我才能解析它 Cocoa是否要求通过视图进行所有拖放? 非常感谢您的帮助 所以要补充我之前发布的内容; 我在上一节学习了拖放图像的示例。它适用于浏览器和图像查找器,但不适用于其他文件类型 附加10/4/2

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

Cocoa是否要求通过视图进行所有拖放? 非常感谢您的帮助

所以要补充我之前发布的内容; 我在上一节学习了拖放图像的示例。它适用于浏览器和图像查找器,但不适用于其他文件类型

附加10/4/2011 如上所述,我遵循上面链接中的拖放示例来拖放基于TIFF的图像。它可以从web或Finder将图像拖动到自定义视图中。我不明白的是,我需要做什么才能让它在简单的文本文件中工作,更重要的是,在自定义文件中工作。我已经阅读了macdev站点上的拖放信息,但仍然没有足够的理解来进行必要的修改

这是我的密码:

//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
//myNSView.h
#进口
@接口MyNSView:NSView
{
NSImage*nsImageObj;
}
@属性(分配)NSImage*nsImageObj;
-(iAction)重置:(id)发送方;
@结束
//myNSV.m
#导入“MyNSView.h”
@实现MyNSView
@合成nsImageObj;
-(id)initWithFrame:(NSRect)帧
{
if(!(self=[super initWithFrame:frame]))
{
NSLog(@“错误:MyNSView initWithFrame”);
回归自我;
}//如果结束
self.nsImageObj=nil;
[self-RegisterOrdRaggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,NSFileNameSPOARDTYPE,nil]];
回归自我;
}//结束与框架
-(void)drawRect:(NSRect)dirtyRect
{
if(self.nsImageObj==nil){
[[NSColor blackColor]set];
NSRectFill(dirtyRect);
}//如果结束
NSRect zOurBounds=[自边界];
[super-drawRect:dirtyRect];
[self.nsImageObj compositepoint:(zOurBounds.origin)操作:NSCompositeSourceOver];
}
-(iAction)重置:(id)发送方{
NSLog(@“重置按钮按下”);
nsImageObj=nil;
NSLog(@“检查图像%@”,self.nsImageObj);
[[NSColor blackColor]set];
[自我设置需要显示:是];
}
-(NSDragOperation)DragginGeted:(id)发送方{
//发件人是否正在查找NSDRAGO
if((NSDragOperationGeneric&[sender DragingSourceOperationMask])==NSDragOperationGeneric)
返回nsdragoOperationGeneric;
其他的
返回nsdragooperationne;
}//端部牵引
-(BOOL)prepareForDragOperation:(id)发送方{
返回YES;
}//结束prepareForDragOperation
-(BOOL)performDragOperation:(id)发送方{
//初始化粘贴板
NSPasteboard*zPasteboard=[发送方拖动PasteBoard];
//初始化图像文件类型,添加一些额外的
NSArray*zImageTypesArray=[NSArray arrayWithObjects:NSPasteboardTypeTIFF,NSFileNamesPardType,nil];
NSString*zDesiredType=[zPasteboard availableTypeFromArray:zImageTypesArray];
if([zDesiredType IsequalString:NSPasteboardTypeTIFF]){
NSData*zPasteboardData=[ZPasteBoardDataForType:zDesiredType];
//确保我们有数据
如果(zPasteboardData==nil){
NSLog(@“错误:MyNsView zPasteBoardData==nil”);
返回否;
}//如果为零,则结束
self.nsImageObj=[[NSImage alloc]initWithData:zPasteboardData];
[自我设置需要显示:是];
返回YES;
}//结束外部if
//如果需要,类型是一个文件名字符串
if([zDesiredType IsequalString:nsFileNamesPardType]){
NSArray*zFileNamesAry=[zPasteboard propertyListForType:@“nsFileNamesPardType”];
NSString*zPath=[ZFileNameary对象索引:0];
NSImage*zNewImage=[[NSImage alloc]initWithContentsOfFile:zPath];
如果(zNewImage==nil){
NSLog(@“错误:MyNSView performdrag操作zNewImage=nil”);
返回否;
}
//否则这里一切都好
self.nsImageObj=zNewImage;
[自我设置需要显示:是];
返回YES;
}//结束外部if
//如果我们到达这里,就会出现未知错误,返回no
NSLog(@“MYNSView performDragOperation中的错误未知”);
返回否;
}
-(void)ConclutedRagOperation:(id)发件人{
[自我设置需要显示:是];
}
@结束
我希望有人能指出我需要学习什么来解决这个问题。也许是我对粘贴板的困惑,也许是我还不知道的另一个组件。和往常一样,我真的很感谢你的帮助


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


按照指南中的说明设置NSFileNamesPardType的视图和注册表。获取文件名时,请使用或使用Spotlight元数据API获取所需文件的任何信息。

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


按照指南中的说明设置NSFileNamesPardType的视图和注册表。获取文件名时,请使用或使用Spotlight元数据API获取所需文件的任何信息。

如果屏幕上有要执行拖放的内容,那么这肯定是NSView?如果屏幕上有要执行拖放的内容,那么这肯定是一个NSView?你能推荐一个链接来显示创建NSView的步骤吗