Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and drop in an NSView

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 ?

like image 374
Red Mak Avatar asked Nov 12 '12 12:11

Red Mak


1 Answers

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

like image 153
Johann Dirdal Avatar answered Oct 11 '22 23:10

Johann Dirdal