Hi all,
I am trying a simple drag and drop application:
- I am creating a CameraIconView (subclass of NSView, containing some image views, text fields and a pop-up button), at run time.
- This view is enclosed within CameraIconEnclosingBox (subclass of NSBox)
- Requirement is: user should be able to drag CameraIconView at some other location in CameraIconEnclosingBox.
To implement my requirements I am doing this:
- 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