【问题标题】:How to enable trash to accept drop from cocoa application?如何使垃圾能够接受可可应用程序的丢弃?
【发布时间】:2013-09-06 15:05:33
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案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:)
    

    【讨论】:

    • 感谢您的代码,很抱歉我回复晚了。我尝试使用此代码从应用程序拖放到垃圾箱,但如果我将文件图像放在桌面上的垃圾箱图标上, - (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination 方法不会被调用。如果我停留在垃圾桶图标上并等待打开垃圾桶窗口,则会调用上述方法,但垃圾桶不接受文件丢弃。请就此向我提出更多建议。
    • 是否有可能如果我将项目拖到应用程序之外,然后将表格中的行拖入转换为图像,以便垃圾接受它,因为仅在拖动图像时才调用拖拽图像方法?
    • 如果你想把图片丢到垃圾桶,那么试试下面的方法:- - (void)drawRect:(NSRect)dirtyRect { NSString *imageName = [[NSBundle mainBundle] pathForResource:@"yourImageName" ofType :@"tiff"]; NSImage *photoImage = [[NSImage alloc] initWithContentsOfFile:imageName]; [imv setImage:photoImage]; } -(void)mouseDown:(NSEvent *)theEvent {NSString *imageName = [[NSBundle mainBundle] pathForResource:@"yourImageName" ofType:@"tiff"]; [self dragFile:imageName fromRect:NSMakeRect(1.0, 1.0, 1.0, 1.0) slideBack:YES event:theEvent];} 因为它是部分执行的。请按照要求做。哟需求。
    • 我可以设法将表格行放入垃圾箱吗?从图像中我的意思是这种方法 - (void)draggedImage:(NSImage *)anImageendedAt:(NSPoint)aPoint operation:(NSDragOperation)operation {
    【解决方案2】:

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

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

    并检查NSDragOperationDelete的操作

    【讨论】:

      猜你喜欢
      • 2010-11-29
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多