Drag and Drop tutorials

Discussion in 'Mac Programming' started by larswik, Jun 30, 2014.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    I am looking to find any tutorial for dragging a text file and dropping it on an NSTextField. Then have the contents of the .txt file populate the NSTextField. I have found lots of youtube videos for drag/drop images to a UIImageView but nothing for text.

    Thanks!
     
  2. ArtOfWarfare, Jun 30, 2014
    Last edited: Jun 30, 2014

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    How about this?

    https://developer.apple.com/library...ngs/CocoaDragAndDrop_DragDropImageView_m.html

    It says that it specifically uses images but can be expanded to other datatypes as well.

    It looks like the important part is to have your view class conform to NSDraggingDestination, with a method like this:

    Code:
    - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
        NSURL* fileURL = [NSURL URLFromPasteboard:[sender draggingPasteboard]];
        // ... load the contents of the file that is pointed to by the URL into the text view.
        return YES;
    }
    There's a few other methods it looks like you might need to implement, too, to fully conform to NSDraggingDestination. See the reference for the protocol here:

    https://developer.apple.com/library...Destination_Protocol/Reference/Reference.html
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    Thanks, I will begin to work my way though that tutorial.
     

Share This Page