How to drag .txt file to obj-c

I'm trying to write some absolutely barebone code where I can drag and drop a simple dot.txt file into NSWindow and read the data (and nothing more interesting than that), but all the examples that I could find using images and NSViews, etc. . The Apple section “Dragging and dropping the contents of the file” in the “Dragging and dropping programming topics for Cocoa” section confirms that dragging to a simple NSWindow (rather than to NSView, etc.) seems and seems to discuss exactly what I'm trying to do, but as a relative newbie I still find my link to images and frames confusing.

Can someone please help me get started by showing me where "registerForDraggedTypes" is, except to turn it on, say "initWithFrame" or "initWithCoder", and for what types to register? As soon as I get a window to recognize my drag and drop, I can worry about another “performDragOperation” and “draggingEntered” later.

Thanks: -)

+5
source share
1 answer

This is part of the code I'm working on. This method can be found when creating a new project.

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

I'm new too. Anyway, it worked for me.

And then it is important. We must connect this object to the window object to handle forwarding messages in this object

MainMenu.xib Interface Builder. Interface Builder "App Delegate" ( ) "" "". ( Inspector) "App Delegate" "Window".

:

-(NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender
{
    return NSDragOperationGeneric;
}
-(BOOL)prepareForDragOperation:(id < NSDraggingInfo >)sender
{
    NSPasteboard* pbrd = [sender draggingPasteboard];
    // Do something here.
    return YES;
}
+6

All Articles