“内容价值”和“内容价值”之间有什么区别?和“内容对象”
我现在正在探索绑定,并且有一个 NSPopUpButton -
它为我提供了“值选择”下的许多绑定选项 - Content
、Content Objects
、Content Value
s,然后是选定对象
、选定值
和选定标签
。有人可以解释一下它们之间的区别吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSPopUpButton 的 Cocoa 绑定参考中对此进行了解释,尽管该参考文献不太清楚。
Content
是一个数组控制器,为弹出按钮提供元素。数组控制器应该绑定到一个数组。为了确定数组中的每个元素如何在弹出按钮中显示,-description
被发送到数组中的每个对象。您可以通过两种方式对此进行自定义:
如果您希望
Selected Object
绑定提供一个与Content
所在的数组控制器管理的数组元素不同的对象绑定,您可以将内容对象绑定到另一个数组控制器。它也可以是相同的数组控制器,但具有不同的键路径;如果您希望弹出按钮选项与绑定
Content
的数组控制器管理的数组中每个元素的描述不同,您可以绑定Content Values< /code> 到另一个数组控制器,该控制器管理其元素包含弹出选项的数组。它也可以是相同的数组控制器,但具有不同的键路径。
一个简单的例子:假设您有以下类:
并且您没有重写
-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
中的数组元素不同的内容,请绑定内容对象
。在弹出按钮中获取当前选择的快速方法:
Content
或Content 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 whichContent
was bound, you can bindContent 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 bindContent 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:
and you haven’t overridden the
-description
method. In this case,-description
is useless and thename
property would be a good choice for the popup options. You’d bind:Content
to an array controller that manages an array ofCustomer
instances, controller keyarrangedObjects
;Content Values
to the same array controller, controller keyarrangedObjects
, model keypathname
.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 selectedCustomer
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 bindContent Objects
to the same array controller, controller keyarrangedObjects
, model keypathphoneNumber
. When a popup option is selected, Cocoa bindings will setphoneNumber
instead of an entireCustomer
instance. In summary: if you don’t bindContent Objects
,Selected Object
represents the original object in the array. If you bindContent Objects
, thenSelected 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 theContent Values
bindings.Quick recipe for providing data to the popup button:
Content
if you have objects (not only strings) that represent the popup options;Content Values
if the options that are shown to the user cannot be obtained viaContent
by sending-description
to the array elements;Content Objects
if you wantSelected Object
to return something different from the array elements fromContent
.Quick recipe for obtaining the current selection in a popup button:
Selected Object
if you want to know the full object (either fromContent
orContent Objects
) representing the current popup selection;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.#Object
指任何符合 KVC 的对象。#ObjectValue
指用于从该对象获取值的关键路径。因此,对于弹出窗口绑定,ContentObjects 将绑定到 或类似内容),因此这这就是 contentValues 绑定的用武之地。这类似于 NSArrayController 的
NSArrayController
的arrangedObjects
。假设 this 指的是字典或托管对象的数组。您无法在弹出窗口中有意义地呈现字典(您会得到description
输出的开头,例如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
'sarrangedObjects
. 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 thedescription
output, e.g<NSCFDictionary...
or similar), so this is where the contentValues binding comes in. This would be something like your NSArrayController'sarrangedObjects.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.