Question

I want to drag a row from my tableview and drop it into any other NSTextField in Mac OS X 10.6, and have a string of text be dropped.

Drag and drop already works within my app (between a NSTableView and an NSBrowser), but I have had no success putting any data on the pasteboard that can accepted by apps other than the source application.

Here's the code I tried, which I thought would be enough to get hte word "hello" to be 'pasted' when I drop into some other NSTextField:

-(BOOL) tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes     toPasteboard:(NSPasteboard *)pboard {

    [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
    [pboard setString:@"hello" forType:NSStringPboardType];

return YES;

}

//--

I never get the cursor that shows me the drop will be accepted, and it just doesn't work.

  • Things I've tried:
    • Using the 10.5 version of the Pasteboard identifier, NSStringPBoardType
    • Using the 10.6 version, NSPasteboardTypeString.
    • Setting the owner = nil, since I'm not providing the data lazily.
    • Using the keyed archiver: [pboard setData:[NSKeyedArchiver archivedRootObject:@"Hello!!"]]

None of the above have worked. I think I have the concepts correct: "Encode data, tell the pasteboard what you've got, then give it the data", but since other apps don't recognize it, I suspect I'm not telling the pasteboard the correct datatype.

Where am I going wrong?

Thanks, Woody

Was it helpful?

Solution

Add this in your controller class's awakeFromNib:

- (void)awakeFromNib {
    [tableView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:NO];
    // [tableView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES];

}

(This is assuming you have an IBOutlet hooked up to your tableView named tableView). Be sure to do this in awakeFromNib or later. (For example, if you were to try to do this in your controller class's init methods, your nib files wouldn't be fully loaded yet, and your IBOutlet's would all be nil and the message would have no effect).

By default, most drag operations will be limited to the local application rather than all applications. The forLocal: parameter specifies whether you are referring to drag operations that are local to the application (within the same app), or non-local, meaning all applications. The line that's commented out is basically what you already have the tableView doing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top