NSPopUpButton:多个值& selectedIndex 绑定

发布于 2024-11-18 17:29:52 字数 631 浏览 1 评论 0原文

上下文:

我有一个与 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 技术交流群。

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

发布评论

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

评论(1

沙沙粒小 2024-11-25 17:29:52

您可能需要编写一些代码,但您仍然可以使用绑定来控制 UI 元素,在本例中为弹出按钮。

这是对我有用的一种方法:

在为数组控制器提供内容的控制器中,定义一个属性,其中包含与表视图中的选择相对应的选择索引集。将其绑定到数组控制器的选择索引集,以便它始终与表视图更新和同步。为了简单起见,我在下面将其称为 fileSelectionIndexSet

然后,定义一个提供弹出按钮索引的属性。下面,我将其称为 styleIndex

您可以将弹出按钮选择索引绑定到此属性。您可能还必须从控制器提供其内容。例如,这将是一个返回静态字符串数组的只读属性。

// Header file, just synthezise in implementation
@property (retain) NSInteger styleIndex;

将控制器注册为其自身 fileSelectionIndexSet 属性的观察者:

// It doesn't have to be awakeFromNib, any method will do if called before
// you need the functionality
-(void)awakeFromNib
{
    [self addObserver:self 
           forKeyPath: @"fileSelectionIndexSet" 
              options:NSKeyValueObservingOptionNew 
              context:NULL];             
}


- (void) observeValueForKeyPath:(NSString *)keyPath
                       ofObject:(id)object
                         change:(NSDictionary *)change
                        context:(void *)context
{
    if ( [keyPath isEqualToString: @"fileSelectionIndexSet"] ) 
    {
        NSInteger index;
        index = ... // Compute value based on current LPFile selection
        self.styleIndex = index;
    }
}

将 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.

// Header file, just synthezise in implementation
@property (retain) NSInteger styleIndex;

Register the controller as an observer of its own fileSelectionIndexSet property:

// It doesn't have to be awakeFromNib, any method will do if called before
// you need the functionality
-(void)awakeFromNib
{
    [self addObserver:self 
           forKeyPath: @"fileSelectionIndexSet" 
              options:NSKeyValueObservingOptionNew 
              context:NULL];             
}


- (void) observeValueForKeyPath:(NSString *)keyPath
                       ofObject:(id)object
                         change:(NSDictionary *)change
                        context:(void *)context
{
    if ( [keyPath isEqualToString: @"fileSelectionIndexSet"] ) 
    {
        NSInteger index;
        index = ... // Compute value based on current LPFile selection
        self.styleIndex = index;
    }
}

Implementing self as an observer of its own property makes the styleIndex property a one-way dependant of the fileSelectionIndexSet.

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.

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