Question

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 ?

Was it helpful?

Solution

"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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top