我在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.
我如何转换它以便我可以使用它?
答案 0 :(得分:1)
您需要先将其键入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";
}
}