“内容值”之间的区别是什么和“内容对象” [英] What's the Difference between "Content Values" and "Content Objects"

查看:124
本文介绍了“内容值”之间的区别是什么和“内容对象”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



它提供了一些选项的值选择下的绑定 - 内容内容对象内容值,然后所选对象所选值,所选标签

这些在 NocopUpButton的Cocoa绑定参考,尽管该参考不太清楚。



内容是一个数组控制器,向弹出按钮提供元素。数组控制器应该绑定到一个数组。为了确定如何在弹出按钮中显示数组中的每个元素, -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 Values, 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 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.

这篇关于“内容值”之间的区别是什么和“内容对象”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆