draggingEntered not firing

758 views Asked by At

I have a subclass of NBox that I would like to drag around a canvas called dragBox. I don't understand why draggingEntered isn't being fired on the following code. I get a nice slideback image, but none of the destination delegates are getting fired. Why?

-(void) awakeFromNib
{
  [[self superview] registerForDraggedTypes:[NSArray     arrayWithObject:NSFilenamesPboardType]];


}
-(void) mouseDown:(NSEvent *)theEvent
{
   [self dragImage:[[NSImage alloc] initWithContentsOfFile:@"/Users/bruce/Desktop/Untitled-1.png"] at:NSMakePoint(32, 32)  offset:NSMakeSize(0,0) event:theEvent pasteboard:[NSPasteboard pasteboardWithName:NSDragPboard] source:self slideBack:YES];




}
-(NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender // validate
{
NSLog(@"Updated");
return [sender draggingSourceOperationMask];

}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
NSLog(@"Drag Entered");

return [sender draggingSourceOperationMask];

}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {

 NSLog(@"Move Box");
[self setFrameOrigin:[sender draggingLocation]];


    return YES;
}

-(BOOL) prepareForDragOperation:(id<NSDraggingInfo>)sender
{NSLog(@"Prepared");
return YES;

}
2

There are 2 answers

1
Ryan Nichols On

In your mouseDown method you are not putting anything in the pasteboard before you initiate the drag operation. The documentation for NSView states you need to add your data on the pasteboard before sending it to that message.

Whatever destination views you have are, or should be, registering for a certain drag type. If your pasteboard does not have any matching data for that type, the destinations will not fire any of the NSDraggingProtocol messages.

1
bc888 On

Solved!

I was using the NSBox as both a destination and source. The events weren't being fired when this was the case. I moved registerDragTypes to the superview, the canvas, and implemented the draggingEntered and performDrag there. It works now...

Bruce