在拖/放操作中传递对象指针

发布于 2024-09-24 06:07:07 字数 1213 浏览 1 评论 0原文

是否可以在拖放操作中将指针传递给对象?

这是场景...

我有一个自定义对象的“NSArray”。为了便于讨论,我们会说这是一个 person 对象的列表。

该列表通过 NSArrayController 绑定到 NSTableViewNSTableView 是一个拖动源,我有一个 NSView 作为拖动目标。

实现拖放时,我可以轻松地从 NSArrayController 中获取当前选定人员的姓名作为拖动过程中的 NSString 并将其传递给接收者NSView

但是,我希望能够传递指向 person 对象的指针,以便接收者可以访问整个对象。

问题似乎在于,拖动操作创建了人员对象的新实例,并且尽管在人员模型中实现了所需的方法,但并未引用所选人员对象。结果我在目的地得到了一个人对象,它只是没有填充来自该对象的数据。

理想情况下,我想要对象指针,因为 NSView 将利用对象引用,以便对对象本身的任何更新都反映在 NSViewNSTableView 中(以及使用该对象的其他地方)。

是否可以在拖放操作中使用对象引用,或者我是否需要传递一些作为人员对象的 iVar 的自定义引用,然后在到达目的地后进行查找? (考虑到 Obj-c 有多少对象引用,这对我来说似乎有点过时)。

我知道目标可以接受来自应用程序外部的拖动操作,但是将该操作指定为本地操作可以解释这一点,不是吗?

我检查了苹果文档,但似乎仍然找不到答案。

拖放编程主题

粘贴板编程指南

非常感谢任何和所有帮助。

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

Here is the scenario...

I have an 'NSArray' of custom objects. For arguments sake we'll say this is a list of person objects.

The list is tied to an NSTableView via an NSArrayController. The NSTableView is a drag source and I have an NSView as a drag destination.

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

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

The problem seems to be that the drag operation creates a new instance of a person object and does not make a reference to the selected person object despite implementing the required methods in the person model. As a result I get a person object at the destination, it just isn't populated with the data from the object.

I ideally I want the object pointer because the NSView will make use of the object reference in order that any updates to the object itself are reflected in both the NSView and the NSTableView (and everywhere else the object is used).

Is it even possible to use the object reference in a drag and drop operation or do I need to pass some custom reference that is an iVar of the person object and then do a lookup once it arrives at the destination? (This would seem a little archaic to me considering how much object referencing Obj-c has).

I understand that a target could accept a drag operation from outside the application, but specifying the operation as a local operation would account for this would it not?

I've checked the apple docs but still don't seem to be able to find an answer.

Drag and Drop Programming Topics

Pasteboard Programming Guide

Any and all help much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-10-01 06:07:07

粘贴板只是不同类型数据的容器,因此您在粘贴板上放置的任何内容都必须可转换为某种类型的数据。

您需要的是为每个 Person 对象提供某种类型的标识符,并将其放在您自己发明的自定义类型标识符下的粘贴板上。一般来说,这样的标识符应该是应用程序的捆绑包标识符(应该是域名加应用程序名称的反转,例如 com.example.surfwriter)加上类型名称(例如 com.example.surfwriter) .tabstop)。

收到掉落物时,请检查是否属于此类。如果它存在,请查找正确的对象并用它执行您应该执行的操作。从表视图拖动到同一表视图应通过将对象移动或复制到新行来实现此目的。

不要使用对象的地址(即对象本身)作为其标识符。如果对象消亡(例如,被删除),则地址将不再有效,或者可能引用不同的对象(可能与原始对象属于同一类,也可能不同)。使用一些你可以查找并且安全地找不到的东西。这适用于剪切/复制和粘贴,而不是拖动,但您可以而且应该使用几乎相同的代码来处理这两种情况。

A pasteboard is just a container for data in different types, so whatever you put on the pasteboard must be convertible to data in some type.

What you need is to give each Person object an identifier of some sort and put that on the pasteboard under a custom type identifier of your own invention. Generally, such an identifier should be the bundle identifier of your application (which should be the reverse of your domain name plus your application name—e.g., com.example.surfwriter) plus the name of the type (e.g., com.example.surfwriter.tabstop).

When receiving a drop, check for this type. If it's present, look up the correct object and do whatever you're supposed to with it. A drag from the table view to the same table view should implement this by moving or copying the object to a new row.

Don't use the address of the object—i.e., the object itself—as its identifier. If the object dies (e.g., is deleted), the address will no longer be valid, or may refer to a different object (which may or may not be of the same class as the original object). Use something that you can look up and safely fail to find. This applies to cut/copy and paste more than to drags, but you can and should handle both with pretty much the same code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文