1

他们是否有任何方式使应用程序能够将项目放入垃圾箱。现在我将项目从 NSTableView 拖放到 NSTableView 和 NSOutlineView。

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

4

2 回答 2

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:)
于 2013-09-06T16:13:37.347 回答
0

在尝试解决不同的问题时 - 将自定义项目拖到垃圾箱并使用namesOfPromisedFilesDroppedAtDestination我发现即使拖动的项目像拖动一样消失,namesOfPromisedFilesDroppedAtDestination也从未被调用过。为了解决这个问题,您可以实现:

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

并检查操作NSDragOperationDelete

于 2016-02-13T16:55:19.647 回答