Cocoa绑定到数组控制器中的特定项目 [英] Cocoa binding to a particular item in an array controller

查看:109
本文介绍了Cocoa绑定到数组控制器中的特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 NSArrayController NSTextField 的值绑定到数组中的特定项目?特别是,我想绑定到数组中第一个项目的属性,如果数组为空,不显示任何内容。

Is it possible using NSArrayController to bind a NSTextField's value to a particular item in the array? In particular, I want to bind to a property on the first item in the array, and show nothing if the array is empty.

使用 arrangeObjects .command 显示只是( - 可能是试图显示一个多行字符串的逗号分隔字符串为每个项目,我只想要第一个。

Using arrangedObjects.command shows just "(" -- presumably it's trying to show a multi-line string with comma-separated strings for each item. I just want the first one.

推荐答案

将文本字段绑定到 selection.command ,并以编程方式将数组控制器的选择索引设置为0。您可能需要在任何时候向内容数组添加或删除项目时将选择索引重新设置为0.

Bind the text field to selection.command, and programmatically set the array controller's selection index to 0. You may need to also re-set the selection index to 0 any time you add or remove items to the content array.

很明显,重新允许用户选择数组控制器中的项目(你需要第二个数组控制器)我假设不是这样,因为如果是,我希望你想显示用户选择的对象,而不是总是第一个对象。

Obviously, this won't work if you're allowing the user to select items within the array controller (you'd need a second array controller). I'm assuming that's not the case, since if it were, I'd expect you to want to show the user-selected object, instead of always the first object.

编辑:更好的是,不要这样做 - 如果数组中第一个对象有一些特殊的状态,一个单独的非数组属性(在保存原始数组的相同对象中,我假定数组控制器从中获取它)保存具有该状态的对象。

Better yet, do nothing like this—if the object that is first in the array has some special status, you should make a separate non-array property (in the same object that holds the original array, from which I assume the array controller is getting it) to hold the object that has that status.

这篇关于Cocoa绑定到数组控制器中的特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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