Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Drag and Drop a '.txt' file in obj-c

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 :-)

like image 203
Bender Avatar asked Jan 10 '10 00:01

Bender


1 Answers

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;
}
like image 61
4 revs Avatar answered Dec 19 '22 02:12

4 revs