NSPopUpButton:多个值& selectedIndex 绑定
上下文:
我有一个与 Core Data 绑定的 NSArrayController,它为 NSTableView 提供行。当用户选择行时,arrayController 的“selectedObjects”属性会发生变化。
现在,每个“selectedObjects”都是一个名为“LPFile”的 Core Data 实体,它具有一个名为“style”的属性,该属性是 0 到 3 之间的整数。“style”属性应对应于 NSPopUpButton 的 selectedIndex。
我的问题:
如果用户选择多行并且与这些行关联的 LPFiles 具有相同的“样式”值,我希望 NSPopUpButton 将其“selectedIndex”属性设置为该值。如果行的对象具有不同的“样式”值,则 NSPopUpButton 应显示空白行。 (当用户选择一种样式时,该空白行应该从 NSPopUpButton 中消失。)
我知道如何通过手动编写代码来实现这一点,如果选择仅限于单行,我可以设置这些绑定,但是如何设置建立绑定来处理多个选定的对象,这些对象可能有也可能没有不同的“样式”值?我在谷歌上搜索了很多,但找不到具体的信息,而且我厌倦了尝试! (注意:我在 IB 中提供了 NSPopUpButton 的内容项,因此我不会将任何内容绑定到按钮的内容绑定。)
Context:
I have an NSArrayController tied to Core Data that supplies rows for an NSTableView. When a user selects rows, the arrayController's "selectedObjects" property changes.
Now, each of those "selectedObjects" is a Core Data entity called "LPFile" that has an attribute called "style", which is an integer from 0 to 3. The "style" attribute should correspond to the selectedIndex of an NSPopUpButton.
My Question:
If a user selects multiple rows AND the LPFiles associated with these rows have the same value for "style", I would like the NSPopUpButton to set its "selectedIndex" property to that value. If the rows' objects have DIFFERENT values for "style", then the NSPopUpButton should display a blank row. (When the user then chooses a style, that blank row should disappear from the NSPopUpButton.)
I know how to achieve this by writing code manually and if selection was limited to a single row I could set up those bindings, but how do I set up the bindings to handle multiple selected objects that may or may not have different values for "style"? I've Googled quite a bit, but can't find specific info and I'm tired of experimenting! (Note: I provide the content items for the NSPopUpButton in IB, so I don't bind anything to the content bindings of the button.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要编写一些代码,但您仍然可以使用绑定来控制 UI 元素,在本例中为弹出按钮。
这是对我有用的一种方法:
在为数组控制器提供内容的控制器中,定义一个属性,其中包含与表视图中的选择相对应的选择索引集。将其绑定到数组控制器的选择索引集,以便它始终与表视图更新和同步。为了简单起见,我在下面将其称为
fileSelectionIndexSet
。然后,定义一个提供弹出按钮索引的属性。下面,我将其称为
styleIndex
。您可以将弹出按钮选择索引绑定到此属性。您可能还必须从控制器提供其内容。例如,这将是一个返回静态字符串数组的只读属性。
将控制器注册为其自身
fileSelectionIndexSet
属性的观察者:将 self 实现为其自身属性的观察者,使得
styleIndex
属性成为fileSelectionIndexSet 的单向依赖项
。这意味着每当用户更改表视图中的选择时,弹出按钮都会更新。但是,当用户更改弹出按钮中的选择时,表视图中不会发生任何更改。
You'll probably have to write a little bit of code, but you can still use bindings to control the UI elements, in this case the popup button.
Here is one way to do it that has worked for me:
In the controller that provides the content for the array controller, define a property which contains the selection index set corresponding to the selection in the table view. Bind it to the array controller's selection index set, so it is always updated and sync'ed with the table view. For simplicity, I have called it
fileSelectionIndexSet
in the following.Then, define a property that provides the index for the popup button. Below, I have called it
styleIndex
.You can bind the popup buttons selection index to this property. You may have to provide its content from the controller, too. That would be a readonly property returning a static array of strings, for instance.
Register the controller as an observer of its own
fileSelectionIndexSet
property:Implementing self as an observer of its own property makes the
styleIndex
property a one-way dependant of thefileSelectionIndexSet
.This means that whenever the user changes the selection in the table view, the popup button is updated. However, when the user changes the selection in the popup button, nothing is changed in the table view.