Cocoa NSView blocks drag and drop

I have a subclass of NSView that registers drag and drop files in the init method, for example:

[self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]]; 

Dragging and dropping works fine, but if I add this view with the same frame, it no longer works. I assume that subview blocks the drag event to go to the super view. Can i avoid this? Thanks

In addition, I know that I am asking two questions, but I do not want to create a new topic just for this: when I drag, my cursor does not change to a + sign, as with other drags, How do I do this? Thanks again.

UPDATE: Here, as I created it in my IB: enter image description here

DrawView is a custom class that I talked about about being registered for draggedtypes. And viewing the image is just a preview, I dragged the image from the multimedia section ... If that helps, here is my corresponding code for DragView:

 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender { return NSDragOperationCopy; } - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender { NSPasteboard *pboard; pboard = [sender draggingPasteboard]; NSArray *list = [pboard propertyListForType:NSFilenamesPboardType]; if ([list count] == 1) { BOOL isDirectory = NO; NSString *fileName = [list objectAtIndex:0]; [[NSFileManager defaultManager] fileExistsAtPath:fileName isDirectory: &isDirectory]; if (isDirectory) { NSLog(@"AHH YEA"); } else { NSLog(@"NOO"); } } return YES; } 
+7
source share
1 answer

The answer to the second part of your question is this:

 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{ return NSDragOperationCopy; } 

if you return NSDragOperationCopy , you will get a mouse icon for the copy operation. (You can and should, of course, not just return NSDragOperationCopy unconditionally, check the objects on the file cabinet to see if you can accept them.)

I am not sure the answer to the first part of your question is because I cannot recreate the subview lock effect.

Well, the answer, unfortunately, you cannot. The image you are dragging is contained in NSImageView and NSImageView accept the drag events on its own, so it captures the drag event and does nothing with it. If your subview was a custom class, you can either a) not implement drag and drop, in which case there will be drag and drop; b) drag and drop to accept the drag for the subquery. In this case, you are using a class that you have no control over. If all you want to do is display the image, you can make another subclass of NSView that does nothing but draw the image in drawRect:

+4
source

All Articles