简体   繁体   English

NSTextView拖放-放置后不可见的字符

[英]NSTextView Drag & Drop — Characters not visible after drop

I've subclasses an NSTextView so that I can drop a file and copy the string contents of the file into the view (as opposed to the standard implementation which drops the filepath into the view). 我将NSTextView子类NSTextView以便可以删除文件并将文件的字符串内容复制到视图中(与将文件路径拖放到视图中的标准实现相反)。 The text seems to be dropping correctly, but then is not visible after the drop. 该文本似乎正确删除,但是删除后不可见。 I can see that the cursor has moved and can even copy the dropped text out of the view and paste into, for example, TextEdit. 我可以看到光标已经移动,甚至可以将拖放的文本复制到视图之外并粘贴到例如TextEdit中。 I tried adding [self setNeedsDisplay:YES] at the end of my -performDragOperation: method, but the behavior did not change. 我尝试在-performDragOperation:方法的末尾添加[self setNeedsDisplay:YES] ,但是行为没有改变。

Here's the code I've written so far. 这是我到目前为止编写的代码。 I imagine this is not the best way to implement this. 我想这不是实现此目标的最佳方法。 I'm new to drag and drop implementation in cocoa. 我是在可可中拖放实现的新手。

-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {

    NSPasteboard *pb = [sender draggingPasteboard];
    NSDragOperation dragOperation = [sender draggingSourceOperationMask];

    if ([[pb types] containsObject:NSFilenamesPboardType]) {
        if (dragOperation & NSDragOperationCopy) {
            return NSDragOperationCopy;
        }
    }
    if ([[pb types] containsObject:NSPasteboardTypeString]) {
        if (dragOperation & NSDragOperationCopy) {
            return NSDragOperationCopy;
        }
    }

    return NSDragOperationNone;

}


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

    NSPasteboard *pb = [sender draggingPasteboard];

    if ( [[pb types] containsObject:NSFilenamesPboardType] ) {
        NSArray *filenames = [pb propertyListForType:NSFilenamesPboardType];

        for (NSString *filename in filenames) {
            NSStringEncoding encoding;
            NSError * error;
            NSString * fileContents = [NSString stringWithContentsOfFile:filename usedEncoding:&encoding error:&error];
            if (error) {
                // handle error
            }
            else {
                [self setString:fileContents];
            }
        }

    }

    else if ( [[pb types] containsObject:NSPasteboardTypeString] ) {
        NSString *draggedString = [pb stringForType:NSPasteboardTypeString];
        [self setString:draggedString];
    }

    return YES;

}

I had a stub for -drawRect: that had no implementation. 我有一个-drawRect:存根-drawRect:没有实现。

After removing the stub, everything works exactly as intended. 删除存根后,所有操作均按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM