刷新Cocoa-Binding - NSArrayController - ComboBox [英] Refresh Cocoa-Binding - NSArrayController - ComboBox

查看:428
本文介绍了刷新Cocoa-Binding - NSArrayController - ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我做了一个非常简单的绑定。我有一个NSMutableArray绑定到一个NSArrayController。控制器本身绑定到一个ComboBox,它显示了NSMutableArray的所有内容。工作正常。



问题是:数组的内容会改变。如果用户对应用程序进行一些调整,我删除NSMuteableArray中的所有项目,并用新的和不同的项目填充它们。



但是NSMutableArray& NSArrayController < - > NSComboBox不刷新。



无论我从数组中删除所有对象,ComboBox仍显示相同的项目。



这里有什么问题?是我的方法错了或者我只需要告诉绑定刷新自己?

解决方案

你可能编辑控制器后面的数组



我有一个NSMutableArray绑定到一个NSArrayController。


如何?阵列在哪里生活?在一个文档中,可以通过KVC / KVO兼容的-myArray / -setMyArray:访问器集合访问?



我打赌你直接告诉myArray到-removeAllObjects,对吧?这些KVC / KVO访问器如何知道数组已经改变了?



答案是,他们没有。如果你真的要替换整个数组,你需要告诉你的文档(或者拥有数组的人)-setMyArray:一个全新的数组。这将触发正确的KVO调用。



...但是,你真的不需要一个可变数组,是吗?如果你只想替换数组中的单个项目,你将需要使用索引访问器:



(文档 - 参见集合访问器模式的多个属性)
http://tinyurl.com/yb2zkr5


in my application I made a very simple binding. I have a NSMutableArray bound to a NSArrayController. The controller itself is bound to a ComboBox and it shows all the content of the NSMutableArray. Works fine.

The problem is : The content of the Array will change. If the user makes some adjustments to the app I delete all the items in the NSMuteableArray and fill it with new and different items.

But the binding of NSMutableArray <-> NSArrayController <-> NSComboBox does not refresh.

No matter if I remove all objects from the Array the ComboBox still shows the same items.

What is wrong here? Is my approach wrong or do I only need to tell the binding to refresh itself? I did not find out how to do that.

解决方案

You're likely "editing the array behind the controller's back", which subverts the KVO mechanism.

You said:

I have a NSMutableArray bound to a NSArrayController.

How? Where does the array live? In a document, accessible via a KVC/KVO compliant -myArray / -setMyArray: set of accessors?

I'll bet you're directly telling the "myArray" ivar to -removeAllObjects, right? How will these KVC/KVO accessors "know" the array has changed?

The answer is, they don't. If you're really replacing the whole array, you'll want to tell your document (or whoever owns the array) to -setMyArray: to a whole new array. This will trigger the proper KVO calls.

... but then, you don't really need a mutable array, do you? If you only want to replace individual items in the array, you'll want to use indexed accessors:

(Documentation - see the Collection Accessor Patterns for To-Many Properties section) http://tinyurl.com/yb2zkr5

这篇关于刷新Cocoa-Binding - NSArrayController - ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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