我的应用程序允许拖动到主窗口和状态项。

  • 如果我将文件从 Stacks 拖到我的窗口中,它就可以完美运行。
  • 如果我将文件从 Finder 拖到我的窗口中,它就可以完美运行。
  • 如果我将文件从 Finder 拖到我的状态项,它就可以完美运行。
  • 如果我将文件从 Stack 拖动到我的状态项,它就不起作用。

  • 窗口和状态项都使用完全相同的拖放处理代码。

    有趣的是,当文件从 Stacks 拖到状态项上时,光标会按预期变化,因为 - (NSDragOperation)draggingEntered:(id )sender {
    NSPasteboard *pboard;
    NSDragOperation sourceDragMask;
    方法按预期调用。

    然而,当文件被删除时, - (BOOL)performDragOperation:(id )sender {
    NSPasteboard *pboard;
    NSDragOperation sourceDragMask;
    方法未被调用。

    下面是第一种方法的实现:
    - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
        NSPasteboard *pboard;
        NSDragOperation sourceDragMask;
    
        sourceDragMask = [sender draggingSourceOperationMask];
        pboard = [sender draggingPasteboard];
    
        if ( [[pboard types] containsObject:NSColorPboardType] ) {
            if (sourceDragMask & NSDragOperationGeneric) {
                return NSDragOperationGeneric;
            }
        }
        if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
            if (sourceDragMask & NSDragOperationLink) {
                return NSDragOperationLink;
            } else if (sourceDragMask & NSDragOperationCopy) {
                return NSDragOperationCopy;
            }
        }
    
        return NSDragOperationNone;
    }
    

    谢谢!

    最佳答案

    这是一个合法的问题。我已为此向 Apple 提交了错误报告。 http://openradar.appspot.com/radar?id=1745403

    与此同时,我想出了一个解决方法。即使 performDragOperation: 从未被调用, draggingEnded: 仍然是。您仍然可以通过检查“draggingLocation”点是否在 NSView 的矩形内来判断文件是否被放置在 NSStatusItem 上。下面是一个例子:

    - (void)draggingEnded:(id<NSDraggingInfo>)sender
    {
        if(NSPointInRect([sender draggingLocation],self.frame)){
            //The file was actually dropped on the view so call the performDrag manually
            [self performDragOperation:sender];
        }
    }
    

    希望这会有所帮助,直到错误得到修复。

    关于cocoa - 奇怪的行为 : dragging from Stacks to status item doesn't work,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9534543/

    10-14 18:37