Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
Xcode 拖放;在Cocoa中放置(<;NSDraggingDestination>;)无效_Xcode_Cocoa_Drag And Drop_Nsdragginginfo - Fatal编程技术网

Xcode 拖放;在Cocoa中放置(<;NSDraggingDestination>;)无效

Xcode 拖放;在Cocoa中放置(<;NSDraggingDestination>;)无效,xcode,cocoa,drag-and-drop,nsdragginginfo,Xcode,Cocoa,Drag And Drop,Nsdragginginfo,我想在我的Mac应用程序中实现一些“简单”的拖放操作。 我拖入了一个视图,并在我的nib文件中相应地输入了“MyView”。但是我在控制台中没有得到任何响应(每当触发任何协议方法时,我都尝试记录消息) 我将NSView分为以下子类: @interface MyView : NSView <NSDraggingDestination>{ NSImageView* imageView; } @接口MyView:NSView{ NSImageView*图像视图; } 并按如下方

我想在我的Mac应用程序中实现一些“简单”的拖放操作。 我拖入了一个视图,并在我的nib文件中相应地输入了“MyView”。但是我在控制台中没有得到任何响应(每当触发任何协议方法时,我都尝试记录消息)

我将NSView分为以下子类:

@interface MyView : NSView <NSDraggingDestination>{
    NSImageView* imageView;
}
@接口MyView:NSView{
NSImageView*图像视图;
}
并按如下方式实施:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self registerForDraggedTypes:[NSImage imagePasteboardTypes]];

        NSRect rect = NSMakeRect(150, 0, 400, 300);
        imageView = [[NSImageView alloc] initWithFrame:rect];

        [imageView setImageScaling:NSScaleNone];
        [imageView setImage:[NSImage imageNamed:@"test.png"]];
        [self addSubview:imageView];



    }

    return self;
}


- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{
    NSLog(@"entered");
    return NSDragOperationCopy;

}

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender{
    return NSDragOperationCopy;

}


- (void)draggingExited:(id <NSDraggingInfo>)sender{
    NSLog(@"exited");

}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender{
    NSLog(@"som");
    return YES;

}

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {

    NSPasteboard *pboard = [sender draggingPasteboard];

    if ( [[pboard types] containsObject:NSURLPboardType] ) {
        NSURL *fileURL = [NSURL URLFromPasteboard:pboard];
        NSLog(@"%@", fileURL);
    }
    return YES;
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender{
    NSLog(@"conclude sth");


}

- (void)draggingEnded:(id <NSDraggingInfo>)sender{
    NSLog(@"ended");


}

- (BOOL)wantsPeriodicDraggingUpdates{
    NSLog(@"wants updates");

}


- (void)updateDraggingItemsForDrag:(id <NSDraggingInfo>)sender NS_AVAILABLE_MAC(10_7){
}
-(id)initWithFrame:(NSRect)frame
{
self=[super initWithFrame:frame];
如果(自我){
[self-RegisterForDragedTypes:[NSImage imagePasteboardTypes];
NSRect rect=NSMakeRect(150,0,400,300);
imageView=[[NSImageView alloc]initWithFrame:rect];
[imageView setImageScaling:nsScaleOne];
[imageView setImage:[nsImageImageName:@“test.png”];
[自添加子视图:图像视图];
}
回归自我;
}
-(NSDragOperation)DragginGeted:(id)发送方{
NSLog(@“输入”);
返回NSDRAGO操作副本;
}
-(NSDRAGO操作)draggingUpdated:(id)发送方{
返回NSDRAGO操作副本;
}
-(无效)拖动退出:(id)发送方{
NSLog(@“已退出”);
}
-(BOOL)prepareForDragOperation:(id)发送方{
NSLog(@“som”);
返回YES;
}
-(BOOL)performDragOperation:(id)发送方{
NSPasteboard*pboard=[发送方拖动粘贴板];
如果([[pboard types]包含对象:NSURLBoard Type]){
NSURL*fileURL=[NSURL URLFromPasteboard:pboard];
NSLog(@“%@”,fileURL);
}
返回YES;
}
-(void)ConclutedRagOperation:(id)发件人{
NSLog(“完成某事”);
}
-(无效)拖动:(id)发送方{
NSLog(@“结束”);
}
-(BOOL)WantsPeriodicDraggingUpdate{
NSLog(@“需要更新”);
}
-(void)updateDraggingItemsForDrag:(id)发送方N\u可用\u MAC(10\u 7){
}

如果仍有人需要,请使用:

[self registerForDraggedTypes:[NSArray arrayWithObjects: 
                       NSFilenamesPboardType, nil]];
而不是:

[self registerForDraggedTypes: 
                      [NSImage imagePasteboardTypes]]; // BAD: dropped image runs away, no draggingdestination protocol methods sent...
帮我解决了这个问题。我怀疑这是有效的,因为我的XIB针对的是osX 10.5。NSFileNamesPardType表示10.5及之前的“标准数据”UTI,[NSImage imagePasteboardTypes]返回10.6及之后的标准数据UTI

顺便说一句,在-(BOOL)performdraginoperation:(id NSDraggingInfo)sender中,您可以获得已删除文件的路径,其中包括:

 NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
这里有解决方案:


imagePasteboardTypes=仅图像内容