“内容值”之间的区别是什么和“内容对象” [英] What's the Difference between "Content Values" and "Content Objects"
问题描述
我现在正在探索绑定,并有一个NSPopUpButton -
它提供了一些选项的值选择下的绑定 - 内容
,内容对象
,内容值
,然后所选对象
,所选值,
和所选标签
。
内容
是一个数组控制器,向弹出按钮提供元素。数组控制器应该绑定到一个数组。为了确定如何在弹出按钮中显示数组中的每个元素, -description
被发送到数组中的每个对象。
您可以通过两种方式进行自定义:
-
如果您希望
对象
绑定以提供与由Content
绑定的数组控制器管理的数组元素不同的对象,您可以绑定Content Objects
复制到另一个阵列控制器。它也可以是相同的数组控制器但是具有不同的键路径; -
如果您想要弹出按钮选项不同于每个元素的描述在由
Content
绑定的数组控制器管理的数组中,您可以将Content Values
绑定到另一个数组控制器管理其元素包含弹出选项的数组。
一个简单的例子:假设你有一个数组控制器,下面的类:
@interface客户:NSObject
@property(copy)NSString * name;
@property(copy)NSString * phoneNumber;
@end
且您尚未覆写
方法。在这种情况下, -description
是无用的,并且 name
属性将是弹出选项的一个不错的选择。您将绑定到一个数组控制器的
-
客户
实例,控制器密钥arrangedObjects
; -
内容值
到同一个数组控制器,控制器键arrangeObjects
,模型keypath名称
/ li>
然后,您可以将 Selected Object
绑定到其他值,例如您的应用程序委托或窗口控制器。 Cocoa绑定会将选定的 Customer
实例分配给该属性。
现在假设你对整个客户
已选择的对象,但只包含其电话号码。在这种情况下,您可以将内容对象
绑定到同一个数组控制器,控制器密钥 arrangedObjects
,model keypath phoneNumber
。当选择弹出选项时,Cocoa绑定将设置 phoneNumber
而不是整个 Customer
实例。总之:如果不绑定内容对象
,所选对象
表示数组中的原始对象。如果绑定内容对象
,则所选对象
可能有所不同。
如果您对原始对象(或内容对象)不感兴趣,则可以绑定 Selected Value
,但是弹出选项中显示的实际字符串到内容值
绑定。
向弹出按钮提供数据的快速配方:
- 如果您有代表弹出选项的对象(不仅仅是字符串),请绑定
Content
; - 绑定
内容值
如果无法通过Content
获得向用户显示的选项向数组元素发送-description
; - 绑定
内容对象
if您要所选对象
返回与内容
不同的数组元素。
在弹出按钮中获取当前选择的快速方法:
- 绑定
或
如果您想要知道完整的对象(
Content
code>)表示当前弹出式选择; - 绑定
选定值
如果您只想要当前在
最后,如果弹出窗口中使用 Selected Tag
选项实际上来自其项目设置了标签的菜单。
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 Value
s, and then Selected Object
, Selected Value,
and Selected Tag
. Could someone please explain the difference between these?
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:
@interface Customer : NSObject
@property (copy) NSString *name;
@property (copy) NSString *phoneNumber;
@end
and you haven’t overridden the -description
method. In this case, -description
is 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 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 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 viaContent
by sending-description
to the array elements; - Bind
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:
- Bind
Selected Object
if you want to know the full object (either fromContent
orContent 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.
这篇关于“内容值”之间的区别是什么和“内容对象”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!