0

我正在通过我的 DisplayContainer 搜索文本字段以更改它们,但我在更改参考时遇到了问题:

    for (var i:uint = 0, n:uint = rootContainer.numChildren; i < n; ++i) {
        var item:DisplayObject = rootContainer.getChildAt(i);
        if (item is DisplayObjectContainer) {
            updateFields(DisplayObjectContainer(item));
        } else if (item is TextField) {
            item.text  = "hej";
        }
    }

item.text 抛出:

1119: Access of possibly undefined property text through a reference with static type flash.display:DisplayObject.

我如何转换它以便我可以使用它?

4

1 回答 1

1

您需要将其键入为 a TextField,然后才能对其调用TextField方法:

for (var i:uint = 0, n:uint = rootContainer.numChildren; i < n; ++i) {
    var item:DisplayObject = rootContainer.getChildAt(i);
    if (item is DisplayObjectContainer) {
        updateFields(DisplayObjectContainer(item));
    } else if (item is TextField) {
        var textField:TextField = item as TextField;
        textField.text  = "hej";
    }
}
于 2012-07-24T17:29:10.070 回答