draggingEntered not called

2.1k views Asked by At

I have an NSBox subclass called dragBox. I want to be able to drag it around a canvas. The code is as follows:

-(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;

}

Why isn't dragEntered being called? I have tried to use all the pboard types and such. Nothing seems to work. I have also changed the registerForDraggedTypes to just work off of the [self] view. The box is a subview of a canvas.

Bruce

2

There are 2 answers

2
Abdul Naveed On

Bruce,

Your Code needs to be changed in the below way. I believe that view should be registered for drag types to make the method draggingEntered to get called.

@interface NSModifiedBox : NSBox

@end


@implementation NSModifiedBox
- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
    [self registerForDraggedTypes:
     [NSArray arrayWithObjects:NSTIFFPboardType,NSFilenamesPboardType,nil]];
    [super drawRect:dirtyRect];
}


- (NSDragOperation)draggingEntered:(id )sender
{
    if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
        == NSDragOperationGeneric)
    {

        return NSDragOperationGeneric;

    } // end if

    // not a drag we can use
    return NSDragOperationNone;

} 

- (BOOL)prepareForDragOperation:(id )sender
{
    return YES;
} 
@end
  1. Now Drag and Drop a NSBox on the Xib and the Modify the class of NSBox to NSModifiedBox.
  2. Set a break point to the method "draggingEntered".
  3. Now Drag a ".png" or ".gif" file and drop on the NSModifiedBox and you see the "draggingEntered" will get invoked
  4. Or you can check by using NSLog as well inside a "draggingEntered".

Hope my answer will help you :)

1
Chris Livdahl On

I found that awakeFromNib was the wrong place to put my registerForDragTypes call since I am programmatically adding my view (i.e. not adding it via a Nib). I had to put the call into initWithFrame:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self registerForDraggedTypes: [NSArray arrayWithObjects:NSTIFFPboardType,NSFilenamesPboardType,nil]];
    }

    return self;
}