“内容价值”和“内容价值”之间有什么区别?和“内容对象”

发布于 2024-12-12 06:35:11 字数 221 浏览 0 评论 0 原文

我现在正在探索绑定,并且有一个 NSPopUpButton -

它为我提供了“值选择”下的许多绑定选项 - ContentContent ObjectsContent Value s,然后是选定对象选定值选定标签。有人可以解释一下它们之间的区别吗?

I'm exploring bindings right now, and have an NSPopUpButton -

It presents me a number of options for bindings under Value Selection - Content, Content Objects, Content Values, and then Selected Object, Selected Value, and Selected Tag. Could someone please explain the difference between these?

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

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

发布评论

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

评论(2

梦中楼上月下 2024-12-19 06:35:11

NSPopUpButton 的 Cocoa 绑定参考中对此进行了解释,尽管该参考文献不太清楚。

Content 是一个数组控制器,为弹出按钮提供元素。数组控制器应该绑定到一个数组。为了确定数组中的每个元素如何在弹出按钮中显示,-description 被发送到数组中的每个对象。

您可以通过两种方式对此进行自定义:

  • 如果您希望 Selected Object 绑定提供一个与 Content 所在的数组控制器管理的数组元素不同的对象绑定,您可以将内容对象绑定到另一个数组控制器。它也可以是相同的数组控制器,但具有不同的键路径;

  • 如果您希望弹出按钮选项与绑定 Content 的数组控制器管理的数组中每个元素的描述不同,您可以绑定 Content Values< /code> 到另一个数组控制器,该控制器管理其元素包含弹出选项的数组。它也可以是相同的数组控制器,但具有不同的键路径。

一个简单的例子:假设您有以下类:

@interface Customer : NSObject
@property (copy) NSString *name;
@property (copy) NSString *phoneNumber;
@end

并且您没有重写 -description 方法。在这种情况下,-description没有用,而name属性对于弹出选项来说是一个不错的选择。您将:

  • Content 绑定到一个数组控制器,该控制器管理 Customer 实例数组,控制器键 arrangedObjects
  • Content Values 到同一数组控制器、控制器键 arrangedObjects、模型键路径 name

然后,您可以将选定对象绑定到其他内容,例如应用程序委托或窗口控制器中的属性。然后,Cocoa 绑定会将选定的 Customer 实例分配给该属性。

现在假设您对所选的整个 Customer 对象不感兴趣,而只对其电话号码感兴趣。在这种情况下,您可以将 Content Objects 绑定到同一数组控制器、控制器键 arrangedObjects、模型键路径 phoneNumber。当选择弹出选项时,Cocoa 绑定将设置 phoneNumber 而不是整个 Customer 实例。总之:如果您不绑定Content Objects,则Selected Object代表数组中的原始对象。如果您绑定内容对象,则选定对象可能会有所不同。

如果您对原始对象(或内容对象)不感兴趣,而是根据内容值在弹出选项中显示的实际字符串,则可以绑定选定值绑定。

向弹出按钮提供数据的快速秘诀:

  • 如果您有表示弹出选项的对象(不仅仅是字符串),则绑定 Content
  • 如果无法通过将 -description 发送到数组元素来通过 Content 获取向用户显示的选项,则绑定 Content Values
  • 如果您希望选定对象返回与Content中的数组元素不同的内容,请绑定内容对象

在弹出按钮中获取当前选择的快速方法:

  • 如果您想了解完整的对象(来自 ContentContent ObjectsSelected Object >) 代表当前弹出选择;
  • 如果您只需要当前在弹出窗口中选择的字符串,请绑定选定值

最后,如果弹出选项实际上取自其项目设置了标签的菜单,则可以使用Selected Tag

Those are explained in the Cocoa Bindings Reference for NSPopUpButton, although that reference is not quite clear.

Content is an array controller that provides elements to the popup button. The array controller should be bound to an array. In order to determine how each element in the array is shown in the popup button, -description is sent to each object in the array.

You may customise this in two ways:

  • If you want the Selected Object binding to provide an object distinct from the array elements managed by the array controller to which Content was bound, you can bind Content Objects to another array controller. It could also be the same array controller but with a different key path;

  • If you want the popup button options to be something different than the description of each element in the array managed by the array controller to which Content was bound, you can bind Content Values to another array controller that manages an array whose elements contain the popup options. It could also be the same array controller but with a different key path.

A simple example: suppose you have the following class:

@interface Customer : NSObject
@property (copy) NSString *name;
@property (copy) NSString *phoneNumber;
@end

and you haven’t overridden the -description method. In this case, -descriptionis useless and the name property would be a good choice for the popup options. You’d bind:

  • Content to an array controller that manages an array of Customer instances, controller key arrangedObjects;
  • Content Values to the same array controller, controller key arrangedObjects, model keypath name.

You can then bind Selected Object to something else, for example a property in your application delegate or window controller. Cocoa bindings would then assign the selected Customer instance to that property.

Now suppose you are not interested in the whole Customer object that’s been selected, but only its phone number. In this case, you can bind Content Objects to the same array controller, controller key arrangedObjects, model keypath phoneNumber. When a popup option is selected, Cocoa bindings will set phoneNumber instead of an entire Customer instance. In summary: if you don’t bind Content Objects, Selected Object represents the original object in the array. If you bind Content Objects, then Selected Object can be something different.

You’d bind Selected Value if you were not interested in the original objects (or the content objects), but the actual strings shown in the popup options according to the Content Values bindings.

Quick recipe for providing data to the popup button:

  • Bind Content if you have objects (not only strings) that represent the popup options;
  • Bind Content Values if the options that are shown to the user cannot be obtained via Content by sending -description to the array elements;
  • Bind Content Objects if you want Selected Object to return something different from the array elements from Content.

Quick recipe for obtaining the current selection in a popup button:

  • Bind Selected Object if you want to know the full object (either from Content or Content Objects) representing the current popup selection;
  • Bind Selected Value if you only want the string that’s currently selected in the popup.

And lastly, you’d use Selected Tag if the popup options are actually taken from a menu whose items have a tag set.

黎夕旧梦 2024-12-19 06:35:11

#Object 指任何符合 KVC 的对象。 #ObjectValue 指用于从该对象获取值的关键路径。

因此,对于弹出窗口绑定,ContentObjects 将绑定到 NSArrayControllerarrangedObjects。假设 this 指的是字典或托管对象的数组。您无法在弹出窗口中有意义地呈现字典(您会得到 description 输出的开头,例如 或类似内容),因此这这就是 contentValues 绑定的用武之地。这类似于 NSArrayController 的 arrangedObjects.name,其中 name 是字典或托管对象中的键。

我希望这会有所帮助,当我开始使用绑定时,我自己也在同样的概念上挣扎。

#Object refers to any KVC-compliant object. #ObjectValue refers to the key path used to get the value from that object.

So, for your pop-up binding, ContentObjects would be bound to, say, an NSArrayController's arrangedObjects. Say this refers to an array of dictionaries or managed objects. You can't meaningfully present a dictionary in a pop-up (you get the start of the description output, e.g <NSCFDictionary... or similar), so this is where the contentValues binding comes in. This would be something like your NSArrayController's arrangedObjects.name, where name is a key from your dictionary or managed object.

I hope this helps, I struggled with the same concept myself when I started with bindings.

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