Implementations details of drag and drop of file path in cocoa.

Posted by foo on Stack Overflow See other posts from Stack Overflow or by foo
Published on 2010-06-05T15:29:00Z Indexed on 2010/06/05 15:32 UTC
Read the original article Hit count: 189

Filed under:
|
|

Hi,

I'm trying to get a part of my window to react to drag and drop. In this case to copy the file path to the file being dropped on the view. I have looked over some documentation but I still have some problems to see exactly where I should implement my methods for drag and drop.

I have seen some examples but almost all of them assumes that I want to drop an image on the view. I'm pretty new to cocoa and Xcode so, I have some problems with seeing the exact relation between interface builder and the generated code.

I have created a subclass to NSView, called drop zone like this:

#import <Cocoa/Cocoa.h>

@interface dropZone : NSView {

}

@end

I then use an NSRect to color it grey like this:

#import "dropZone.h"

@implementation dropZone


- (void)drawRect:(NSRect)rect
{

    NSRect bounds = [self bounds];
    [[NSColor grayColor] set];
    [NSBezierPath fillRect:bounds];
}
@end

I then drag this class to my window in interface builder. But I will need to implement the registerForDraggingTypes method, but where? I have also seen a convenience method in the Apple docs for file paths like this: dragFile:fromRect:slideBack:event: that might work.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa