1

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

Jay
  • 6,572
  • 3
  • 37
  • 65
bc888
  • 141
  • 14

2 Answers2

4

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;
}
Chris Livdahl
  • 4,662
  • 2
  • 38
  • 33
  • `awakeFromNib` is only called on objects created automatically when deserializing a NIB file. It is never called for objects created programmatically. Also not when these objects are added to objects from a NIB file. The purpose of `awakeFromNib` is to give developers a chance to perform initialization on UI loaded from NIB files that can only be done programmatically but needs to run before the UI is used in any way (before it is used programmatically as well as before it is used by the user). And your "this post" link is broken, that's why only linking to external sources is discouraged. – Mecki Sep 05 '18 at 20:44
1

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 :)

Abdul Naveed
  • 567
  • 2
  • 9
  • 2
    Registering every time `drawRect:` is called is a bad idea. Set a breakpoint here to see what I mean. It gets called a ton. You should do this in `initWithFrame:` instead. – Sam Soffes Mar 24 '15 at 02:46
  • 1
    Agreed with "Sam Soffes", Code can be altered and registered in "initWithFrame". DrawRect will be invoked every time when ever there is a refresh on the view and also will registered again even though it is – Abdul Naveed Mar 31 '15 at 07:04