NSArrayController 上的 SelectionIndexes 仅返回一个值

发布于 2024-11-18 17:30:39 字数 1761 浏览 2 评论 0原文

我有一个 NSCollectionView ,其内容由 NSArrayController 处理。 NSCollectionView 是可选择的,我需要检索所选元素的列表。 我试图观察 NSArrayController “selectionIndexes” 的关键属性,但它总是返回 CollectionView 中第一个元素的值,而不是所选项目的值。

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualTo:@"selectionIndexes"])
    {
        //True if in the array controller of the collection view really exists at least a selected object
        if([[arrayController selectedObjects] count] > 0)
        {
            NSLog(@"Selected objects: %@", [arrayController selectedObjects]);
        }
        else
        {
            NSLog(@"Observer called but no objects where selected.");
        }
    }
}

更新

如果我手动调用 NSLog(@"Selected objects: %@", [arrayController selectedObjects]) 我永远不会调用此方法,

结果总是这样

END UPDATE

2011-07-05 20:44:45.711 collectionView2[2153:903] Selected objects 1: (
    "<Hormiga: 0x10013e330>"
)

我认为我在将 NSArrayController 与 NSCollectionView 绑定时做了一些错误的事情。我可能有什么错? 如果您需要更多信息,请告诉我,如果您需要,我什至可以将整个程序以 zip 形式发布。

更新2

这是我在控制器中用来观察arrayController“selectionIndexes”键的代码。

[arrayController addObserver:self forKeyPath:@"selectionIndexes" options:NSKeyValueObservingOptionNew context:nil];

更新3

问题之一已修复,我忘记设置 NSArrayController 和 NSCollectionView 之间相对于键“selectionIndexes”的绑定。现在我可以手动检索 selectedObject 的列表及其正确的!

我的最后一个问题是当选择索引更改时我没有收到通知。 因此,observeValueForKeyPath:ofObject:change:context: 永远不会被调用!

UPDATE 4

我试图在控制器的 init 方法中设置观察者,但这样 arrayController 仍然为空。将 addObserver 移动到 awakeForNib 中解决了我所有的问题!

I have a NSCollectionView wich content is handled by a NSArrayController.
The NSCollectionView is selectable and I need to retrieve a list of selected elements.
I'm trying to observe key property of NSArrayController "selectionIndexes" but it just return me ALWAYS the value of the first element in CollectionView and not the selected items.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualTo:@"selectionIndexes"])
    {
        //True if in the array controller of the collection view really exists at least a selected object
        if([[arrayController selectedObjects] count] > 0)
        {
            NSLog(@"Selected objects: %@", [arrayController selectedObjects]);
        }
        else
        {
            NSLog(@"Observer called but no objects where selected.");
        }
    }
}

UPDATE

I never get this method called, if I manually invoke NSLog(@"Selected objects: %@", [arrayController selectedObjects]) I get this

The result is always something like this

END UPDATE

2011-07-05 20:44:45.711 collectionView2[2153:903] Selected objects 1: (
    "<Hormiga: 0x10013e330>"
)

I think I have done something wrong binding NSArrayController with NSCollectionView. What could be my fault?
Tell me if you want more info, I can even post the entire program in a zip if you need it.

UPDATE 2

This is the code I use in my controller to observe the arrayController "selectionIndexes" key.

[arrayController addObserver:self forKeyPath:@"selectionIndexes" options:NSKeyValueObservingOptionNew context:nil];

UPDATE 3

One of the problem was fixed, I forgot to set the binding between NSArrayController and NSCollectionView relative to the key "selectionIndexes". Now I can retrieve manually the list of selectedObject and its correct!

My final problem is that I don't receive the notification when selectionIndexes changes.
So observeValueForKeyPath:ofObject:change:context: never get called!

UPDATE 4

I was trying to set the observer in the init method of my controller, but in this way the arrayController is still null. Moving the addObserver in the awakeForNib resolved all my problems!

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

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

发布评论

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

评论(1

淡淡绿茶香 2024-11-25 17:30:39

如果您想保持数组控制器的选择索引与集合视图的同步,您还需要绑定它们。总结:

  • 将集合视图 Content 绑定到数组控制器,键 arrangedObjects
  • 将集合视图 Selection Indexes 绑定到数组控制器,键 选择索引

另外,请确保在添加观察者之前已设置arrayController。保证在 -awakeFromNib 以及之后调用的其他方法中设置 Outlet:如果您使用的是窗口控制器,则可以使用 -windowDidLoad;如果您使用视图控制器,则可以使用 -loadView;否则,应用程序委托中的 -applicationDidFinishLaunching:

If you want to keep the array controller’s selection indexes in sync with the collection view’s, you need to bind them as well. In summary:

  • Bind the collection view Content to the array controller, key arrangedObjects
  • Bind the collection view Selection Indexes to the array controller, key selectionIndexes.

Also, make sure arrayController has been set before you add an observer. Outlets are guaranteed to be set in -awakeFromNib and other methods that are invoked after it: If you’re using a window controller, you can use -windowDidLoad; if you’re using a view controller, you can use -loadView; otherwise, -applicationDidFinishLaunching: in your application delegate.

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