views:

116

answers:

2

Okay, I've got a custom class called "Task", which represents a task to be done. I've got an NSMatrix which acts as a calendar. I want the user to be able to drag an icon from an nscollectionview (I've had no trouble setting up the nscollectionview) onto a cell in the nsmatrix, thereby assigning that task to that day. I just can't seem to get the nsmatrix to respond to the drag or the drop at all.

I've implemented the method:

- (BOOL)collectionView:(NSCollectionView *)cv writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard
{
    [pasteboard declareTypes:[NSArray arrayWithObject:TASK_UTI]  owner:self];
    NSUInteger index=[indexes firstIndex];
    Task* task=[[cv content] objectAtIndex:index];
    NSData* taskData=[NSKeyedArchiver archivedDataWithRootObject:task];
    [taskData retain];
    BOOL success=[pasteboard setData:taskData forType:TASK_UTI];

    return success;
}

in my nscollectionview delegate as shown above. I've sent [self registerForDraggedTypes:[NSArray arrayWithObjects:TASK_UTI, nil]] in my NSMatrix subclass (called "Calendar"). I've implemented the methods:

- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender
- (BOOL)prepareForDragOperation:(id < NSDraggingInfo >)sender
- (BOOL)performDragOperation:(id < NSDraggingInfo >)sender

in my Calendar (NSMatrix subclass) class.

Some debugging shows that the NSMatrix/Calendar object is not even running the dragging methods above. What gives?

A: 

Did you define your Calendar class to implement dragging destination protocol?

Eimantas
What exactly do you mean? I wrote in the question that I implemented the 3 methods:- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender- (BOOL)prepareForDragOperation:(id < NSDraggingInfo >)sender- (BOOL)performDragOperation:(id < NSDraggingInfo >)senderIs there something else I have to do?
Joe Atkin
My bad. I knew something was missing (but wasn't quite sure so I went for the guess). Here's the thing that you're missing (excerpt from apple documentation:===To receive drag operations, you must register the pasteboard types that your window or view will accept by sending the object a registerForDraggedTypes: message, defined in both NSWindow and NSView, and implement several methods from the NSDraggingDestination protocol===Whole drag'n'drop guide is here: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/DragandDrop/DragandDrop.html
Eimantas
I did send it that message, see comment above
Joe Atkin
I've spent hours poring over the drag n' drop guide, with no success.Maybe there's something wrong with my UTI??
Joe Atkin
A: 

First off, you should use your own domain name, not “com.yourcompany”, in the UTI.

Second, did you export the UTI in your Info.plist?

Peter Hosey
Thanks. I do think the problem is with the UTI, but the Apple Docs haven't helped me at all. They don't make it clear on how to set up your own UTI. I've tried exporting it in my info.plist, but I'm not sure I did it right. Could you explain EXACTLY what I need to do to set up my own UTI?
Joe Atkin
I've checked it out, the problem is for sure the UTI--Everything works if I change the "TASK_UTI" to "NSPasteBoardStringType" in all the places.
Joe Atkin
Joe Atkin: From the chapter I linked to: “In addition to declaring the UTI string, the declaration can contain any of the following properties…” implying that only the UTI itself is required. If that's not working, you should edit your question to include the declaration.
Peter Hosey