0

我已经开始使用 Dashcode 为我们的一些 Cocoa 工具编写一个显示报告数据的界面。我正在使用 Dashcode 数据源和绑定来填充 WebView 中的元素,到目前为止一切似乎都运行良好。

我的 dataSource 中的一个对象是我想以编程方式操作的对象数组。我可以很好地更改数组中的对象值,但是如果我想替换数组或数组中的任何对象,我的绑定表将无法观察添加的对象。

这是我认为可以让我轻松地用新内容替换绑定数组的代码:

var dataSource = dashcode.getDataSource("reportData");
var newDetailArray = testArray();
dataSource.setValueForKeyPath(newDetailArray, "content.detailArray");

但这会引发异常:

Exception while binding "content" to keypath "arrangedObjects " TypeError: Result of expression 'this.object.valueForKeyPath' [undefined] is not a function.

有什么我遗漏的东西可以让我轻松地以编程方式操作数组的内容吗?

4

1 回答 1

0

这是问题的解决方案:

1)首先在main.js中定义一个KVO对象。这一步很重要,因为给定数据源的数据必须是可绑定的:

anObj = Class.create(DC.KVO, {
    constructor: function(name) {
        this.name = name;
    }
});

2) 创建一个包含属于“anObj”类的对象的数组:

function switchContent(event)
{   
    var myPerson = new anObj('Paul');
    var myArray = new Array();
    myArray.addObject(myPerson);

    // 'dataSource' has to be defined in Dashcode as usual
    var ds = dashcode.getDataSource('dataSource');

    // replace content of datasource ds with myArray
    ds.setContent(myArray);   
}

我希望这个信息帮助!

于 2011-02-13T15:21:12.207 回答