views:

30

answers:

1

Hi all,

I am trying a simple drag and drop application:

  1. I am creating a CameraIconView (subclass of NSView, containing some image views, text fields and a pop-up button), at run time.
  2. This view is enclosed within CameraIconEnclosingBox (subclass of NSBox)
  3. Requirement is: user should be able to drag CameraIconView at some other location in CameraIconEnclosingBox.

To implement my requirements I am doing this:

  1. Implemented following method in CameraIconView class-

-(void)mouseDown:(NSEvent *)e{

    NSPoint location; 
    NSSize size;
    NSPasteboard *pb = [NSPasteboard pasteboardWithName:@"CameraIconContainer"];

    location.x =  ([self bounds].size.width - size.width)/2;
    location.y =  ([self bounds].size.height - size.height)/2;

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
    [pb declareTypes:[NSArray arrayWithObject:IconDragDataType] owner:self];
    [pb setData:data forType:IconDragDataType];
    [self dragImage:[NSImage imageNamed:@"camera_icon.png"] at:location offset:NSZeroSize event:e pasteboard:pb source:self slideBack:NO];
}

2. Implemented following method in CameraIconEnclosingBox class-

- (void)awakeFromNib{
    [self registerForDraggedTypes:[NSArray arrayWithObject:IconDragDataType]];
}
- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender{
    return NSDragOperationEvery;
}
- (BOOL)performDragOperation:(id < NSDraggingInfo >)sender{
    return YES;
}
- (void)concludeDragOperation:(id < NSDraggingInfo >)sender{
    NSPoint dropLocation = [sender draggingLocation];

    float x = dropLocation.x;
    float y = dropLocation.y;
    NSLog(@"dragOperationConcluded! draggingLocation: (%f, %f)",x,y);

    NSPasteboard *pb = [sender draggingPasteboard];
    NSLog(@"Pasteboard name- %@",[pb name]);
    NSData *draggedData = [pb dataForType:IconDragDataType];

    CameraIconView *object = [NSKeyedUnarchiver unarchiveObjectWithData:draggedData];
    NSLog(@"object - %@ / cameraNo : %@/ operatorName : %@",object,[[object cameraNo] stringValue],[[object operatorName] stringValue]);
    // the cameraNo and operatorName are properties defined within CameraIconView class
    // the above log is returning (null) for both properties

    float width  = [object frame].size.width;
    float height = [object frame].size.height;

    NSLog(@"width - %f / height - %f",width,height);
    [object setFrame:NSMakeRect(x, y, width, height)];
}

After implementing these methods I am able to perform drag but drop operation is not working, although all dragging delegate methods in CameraIconEnclosingBox are called.

Can anyone suggest where I may be wrong or some other better way to implement my requirements?

Thanks,

Miraaj

A: 

Hi all,

I am able to resolve it by adding this line of code:

[self addSubview:object];

before this code:

[object setFrame:NSMakeRect(x, y, width, height)];

So CameraIconView now is appearing at dropped location. :)

... but now I am facing a new problem-

each time the CameraIconView is dragged and dropped, it is not positioning the dragged CameraIconView to new location, but is creating and showing a new instance within CameraIconEnclosingBox at new location, ie. for the first drag and drop two icons start appearing for the same CameraIconView ... for second drag and drop three icons start appearing and so on....

Can anyone suggest me some way to resolve it?

Thanks,

Miraaj

Miraaj