views:

150

answers:

1

I'm trying to write some absolutely barebones code where I can drag a plain 'dot.txt' file onto an NSWindow and read in the data (and nothing fancier than that), but all the examples I've been able to find use images and NSViews etc.. Apple's 'Dragging File Contents' section in its "Drag and Drop Programming Topics for Cocoa" documentation confirms that dragging onto a plain NSWindow (rather than into an NSView etc.) is possible and seems to discuss exactly what I'm trying to do, but as a relative newbie I still find its reference to images and frames confusing.

Can anyone please help me get started by showing me where to 'registerForDraggedTypes' other than putting it in say, an 'initWithFrame' or 'initWithCoder' method, and what types to register for? Once I get the window to recognise my drag I can worry about the other 'performDragOperation' and 'draggingEntered' stuff later.

Thanks :-)

+2  A: 

This is a part of code which I'm working on. You can find this method when you created a new project.

-(void)applicationDidFinishLaunching:(NSNotification*)aNotification
{       
    [window registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]]; 
}

I'm newbie too. Anyway, this worked for me.

And then, this is important. We have to connect this object to window object to handle drop messages in this object

Open MainMenu.xib, and Interface Builder will pop up. In Interface Builder, connect 'App Delegate'(this object) to 'delegate' outlet of 'Window'. (You can find 'delegate' outlet in Inspector panel) Now, 'App Delegate' receives event messages of 'Window'.

And then, adds more method at the same class file:

-(NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender
{
    return NSDragOperationGeneric;
}
-(BOOL)prepareForDragOperation:(id < NSDraggingInfo >)sender
{
    NSPasteboard* pbrd = [sender draggingPasteboard];
    // Do something here.
    return YES;
}
Eonil
This is OK for the specific case where you have an application delegate which has a `window` outlet, but normally a window would be managed by an `NSWindowController` and you'd register the drag types in the window controller's `-windowDidLoad` method. The answer is correct in general though.
Rob Keniger