Pass object pointer in drag mode

Is it possible to pass a pointer to an object in a drag operation?

Here is the script ...

I have an "NSArray" of custom objects. For arguments, we will say that this is a list of human objects.

The list is bound to an NSTableView through an NSArrayController . NSTableView is a drag source, and there is an NSView as a drag NSView .

When performing a drag and drop, I can easily get the name of the currently selected person (s) from NSArrayController as an NSString in the drag and drop process and pass it to the recipient in NSView .

However, I would like to be able to pass a pointer to the person object so that the recipient has access to the entire object.

It seems that the problem is that the drag and drop operation creates a new instance of the person object and does not make a reference to the selected person object, despite the fact that it implements the necessary methods in the user model. As a result, I get the person object at the destination, it just is not populated with data from the object.

Ideally, I want an object pointer, because NSView will use a reference to the object so that any updates to the object itself are reflected in both NSView and NSTableView (and wherever the object is used).

Is it possible to use a link to an object in a drag operation, or do I need to pass some kind of custom link, which is an iVar of the person object, and then search after it arrives at the destination? (It seems a bit archaic to me, given how many objects are associated with Obj-c).

I understand that a target can accept a drag operation from outside the application, but specifying the operation as a local operation will take this into account, right?

I checked the Apple docs but didn't seem to find the answer.

Drag and Drop Programming Topics

Wireframe Programming Guide

Any and all help to value highly.

+4
source share
1 answer

Cardboard is just a container for data of different types, so everything that you put in cardboard should be converted to data in some type.

You need to provide each Person object with an identifier of a certain type and place it on a file cabinet under the special identifier type of your own invention. Typically, such an identifier should be the identifier of the bundle of your application (which should be the reverse of your domain name and your application name, for example, com.example.surfwriter) plus a type name (for example, com.example.surfwriter.tabstop).

Upon receipt of a drop, check this type. If it is present, find the correct object and do whatever you need. Dragging and dropping from the table view into the same table view should realize this by moving or copying the object to a new row.

Do not use the address of the object โ€” ie, the object itself as its identifier. If an object dies (for example, it is deleted), the address will no longer be valid or may refer to another object (which may or may not be of the same class as the original object). Use what you can find and not find safely. This applies to cut / copy and paste more than drag and drop, but you can and should handle both with almost the same code.

+3
source

All Articles