Cocoa:NSArrayController 的自定义控制器密钥

发布于 2024-09-24 04:01:10 字数 914 浏览 5 评论 0原文

是否可以为 NSArrayController 创建自定义控制器密钥?有“arrangedObjects”、“selection”等键...是否可以实现您自己的?我似乎在任何地方都找不到这方面的任何信息。

我想要这样做的原因(除了它看起来像是能够做的有用的事情之外)是:

我有实体“Car”,具有属性“颜色”和“里程”,以及一个数组控制器“CarArrayController”,其中包含这些对象并将它们显示在表格中。在表格下方,我想要两个文本字段,一个显示红色汽车总里程,另一个显示蓝色汽车总里程。显然我可以显示所有汽车的总数: 将值绑定到“[电子邮件受保护]< /a>”,但到目前为止,如果不实现更多阵列或阵列控制器,我无法获得红色和蓝色汽车的单独总和。

如果我可以创建一个控制器密钥“arrangedRedCarObjects”,它显然只返回红色汽车,我可以绑定到:“[电子邮件&n bsp;受保护]”。

我似乎找不到任何方法来实现我的目标:(

必须有一种方法来获得我需要的结果(这似乎是一个通用问题),请帮忙!

干杯,

奥利

Is it possible to create a custom controller key for an NSArrayController? There are keys for "arrangedObjects", "selection" etc... Is it possible to implement your own? I can't seem to find any info on this anywhere.

My reason for wanting to do this (besides it seeming like a useful thing to be able to do) is:

I have Entity "Car", with Attributes "color" and "mileage", and an array controller, "CarArrayController" which holds these objects and displays them in a table. Below the table I want two text fields one displaying total red car mileage and the other total blue car mileage. Obviously I can display the total of all the cars: Bind Value To "[email protected]", but so far I have been unable to get separate sums for red and blue cars without implementing more arrays or array controllers.

If I could create a Controller Key "arrangedRedCarObjects" which obviously only returned the red cars, I could have a binding to: "[email protected]".

I can't seem to find any way to achieve my goal :(

There must be a way to get the result I need (it seems like a generic problem), please help!

Cheers,

Oli

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

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

发布评论

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

评论(2

依 靠 2024-10-01 04:01:10

控制器键不仅仅是字符串:它们是 NSArrayController 的方法,每个方法都会经历一些逻辑,创建一个对象并返回该对象。您不能仅仅添加键,因为键后面没有任何逻辑。

所以,如果你不想制造更多的阵列控制器,你就需要制造一个功能更强大的阵列控制器。创建一个 NSArrayController 的子类,它实现您想要的键的方法,并在这些方法中,询问您自己的源数组(例如,arrangedObjects),执行您想要的任何限制和重新排列,然后返回该结果。

不要忘记将新方法声明为依赖于其源属性(例如,arrangedRedCarObjects 依赖于 arrangedObjects)。这是仅将密钥添加到列表中无法完成的另一件事:它不知道您的新密钥应该依赖于哪个源属性。

The controller keys are more than just strings: They are methods of NSArrayController, each of which goes through some logic, creates an object, and returns that object. You can't just add keys because there wouldn't be any logic behind the key.

So, if you don't want to make more array controllers, you need to make a more capable array controller. Make a subclass of NSArrayController that implements methods for the keys you want, and in those methods, ask yourself for the source array (e.g., arrangedObjects), perform whatever restriction and rearrangement you want, and return that result.

Don't forget to declare your new methods as dependent on their source properties (e.g., arrangedRedCarObjects as dependent on arrangedObjects). That's another thing that just adding keys to a list wouldn't be able to accomplish: It wouldn't know what source property/-ies your new key should depend on.

街角卖回忆 2024-10-01 04:01:10

如果您有办法询问汽车是红色还是蓝色,您可以创建两个由主控制器提供的辅助数组控制器(将辅助控制器的内容数组绑定到主控制器的 arrangedObjects),每个控制器都有一个过滤器将数组限制为汽车的正确颜色的谓词。

If you have some way to ask a car whether it is red or blue, you can create two secondary array controllers fed by the primary (bind secondary controller's content array to the primary controller's arrangedObjects) each with a filter predicate that restricts the array to the proper color of car.

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