5

I'm testing drag and drop in an NSView, but draggingEntered: is never called.

Code:

#import <Cocoa/Cocoa.h>
@interface testViewDrag : NSView <NSDraggingDestination>
@end

@implementation testViewDrag
- (id)initWithFrame:(NSRect)frame
{
   self = [super initWithFrame:frame];
   if (self)
{
    [self registerForDraggedTypes:[NSImage imagePasteboardTypes]];
    NSLog(@"initWithFrame");
}
return self;
}

-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{
NSLog(@"draggingEntered");
return NSDragOperationEvery;
}

-(NSDragOperation) draggingUpdated:(id<NSDraggingInfo>)sender
{
  NSLog(@"draggingUpdated");
  return NSDragOperationEvery;
}
@end

In interface builder I add a subView (class is set to testViewDrag) to the main window. In log I can see the initWithFrame log but when I drag nothing is shown in the log.

What am I missing ?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Red Mak
  • 1,176
  • 2
  • 25
  • 56

1 Answers1

9

"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. During a dragging session, a candidate destination receives NSDraggingDestination messages only if the destination is registered for a pasteboard type that matches the type of the pasteboard data being dragged. The destination receives these messages as an image enters, moves around inside, and then exits or is released within the destination’s boundaries." You can read more about the drag and drop programming topic here. As I see it, your problem lies in the argument you define in your registerForDraggedTypes: method.

Try replacing it with this:

[self registerForDraggedTypes:[NSArray arrayWithObjects:
        NSColorPboardType, NSFilenamesPboardType, nil]];

Hope this helps!

Johann Dirdal
  • 1,100
  • 6
  • 12
  • thank you that work like a charm, if you can now tell me how to get paths of dragged files :) – Red Mak Nov 12 '12 at 12:45
  • 3
    For example, - (BOOL)performDragOperation:(id < NSDraggingInfo >)sender { NSArray * thefiles = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType]; // Handle whatever you want here } – Johann Dirdal Nov 12 '12 at 12:51
  • i really appreciate your help, that was the las point in my application, thank you very much. – Red Mak Nov 12 '12 at 12:56