如何启用垃圾桶以接受来自可可应用程序的丢弃?

时间:2013-09-06 15:05:34

标签: objective-c cocoa drag-and-drop nstableview

他们是否有办法让应用程序将项目放入垃圾箱。现在我将项目从NSTableView拖放到NSTableView和NSOutlineView。

我在这里只有一个问题How to enable drop something on the Trash in objective-c?但是没有发生垃圾。当我拖动项目大纲应用程序时,我得到NSDragOperation的0。 提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

    For dropping outside the location on your machine use the below DataSource methods:-
    - (BOOL)tableView:(NSTableView *)tv writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
    {
        NSArray* entityArray=[yourArrayController selectedObjects];
        NSString* filename=[[[entityArray objectAtIndex:0]valueForKey:@"fileName"]stringValue];
        [pboard setPropertyList:[NSArray arrayWithObject:filename] forType:(NSString*)kPasteboardTypeFileURLPromise];
        NSPoint dragPosition;
        NSRect imageLocation;

        NSEvent *theEvent   = [NSApp currentEvent];

        dragPosition = [tv convertPoint:[theEvent locationInWindow] fromView: nil];
        dragPosition.x -= 16;
        dragPosition.y -= 16;
        imageLocation.origin = dragPosition;
        imageLocation.size = NSMakeSize(32,32);
        [tv dragPromisedFilesOfTypes:[NSArray arrayWithObject:@"*"]
                            fromRect:imageLocation
                              source:self
                           slideBack:YES
                               event:theEvent];
        return YES;
    }

    //For Enabled the Dropping and Extracting the File Path. This method will give you exact path
    - (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination
    {
        NSString *str=[dropDestination path];
        NSLog(@"%@",str);
        NSMutableArray  *rootDraggedItemsNames = nil;
        return rootDraggedItemsNames;
    }

    //When Drag Files accepted to the Destination, then it Start Download the Files. This will accept the file drops on the destination
    - (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
    {
        [[NSPasteboard pasteboardWithName:NSDragPboard] declareTypes:[NSArray arrayWithObject:(NSString*)kPasteboardTypeFileURLPromise] owner:self];
    }

    The below method is for registering the file promises

   -(void)setAcceptDrops:(BOOL)acceptDrops
{
    if (acceptDrops)
    {
        [tableView registerForDraggedTypes: [NSArray arrayWithObjects:(NSString*)kPasteboardTypeFileURLPromise, nil]];
        [tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];

    }
    else
    {
        [tableView registerForDraggedTypes: [NSArray arrayWithObjects:nil]];
        [tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
    }
}

-(void)awakeFromNib
{
    [self setAcceptDrops:YES];
}

Hope it helps:)

答案 1 :(得分:0)

在尝试解决其他问题时 - 将自定义项目拖到垃圾箱并使用namesOfPromisedFilesDroppedAtDestination我发现即使被拖动的项目像拖动工作namesOfPromisedFilesDroppedAtDestination一样也没有被调用。要解决这个问题,您可以实施:

-(void)draggedImage:(NSImage *)anImage
             endedAt:(NSPoint)aPoint
           operation:(NSDragOperation)operation

并检查NSDragOperationDelete

的操作