2012-10-25 117 views
0

我使用Flex 4.6,有一个AIR应用程序getElementByName似乎不工作

我一张有许多标签的面板。我需要从另一个班级访问这些标签。 我不想传递所有的标签对象。所以我想通过面板容器,然后通过getElementByName()方法访问标签。

这里是我的标记:

<s:VGroup includeIn="SLA_textView" width="100%" height="100%" 
paddingBottom="10" paddingRight="10"> 
<s:Panel id="SlaTextViewPanel" width="100%" height="100%" 
title="test View"> 
    <s:Label id="lbTotalBooks" name="test" x="82" y="62" /> 
</s:Panel> 
</s:VGroup> 

ActionScript代码如下所示:

getLabels(Container:Panel){ 
    var _Container:Panel = Container; 

//var tempLabel:Label = _Container.getChildByName("test") as Label; 

var n:int = _Container.numChildren; 
    for (var i:int = 0; i < n; i++) { 
    var c:DisplayObject = _Container.getChildAt(i); 
    trace(c.name); 
} 

var tempLabel:Label = _Container.getChildByName("test") as Label; 

为什么tempLabel总是null

所以我试图循环通过面板上的对象的名称和测试标签甚至不显示?所以我在某个地方出了问题。

跟踪:

instance1333 
PanelSkin1074 
+1

你不应该使用像'getChild)与Spark容器的方法(',因为这个标签是不是该小组的直接子幕后;你应该使用'getElement()'等。 'name'也是Flash API的遗物。无论如何你为什么需要这个?我想不出任何理由。 – RIAstar

+0

我有什么是从C#返回一个表的Web服务。返回监听器在类中。所以我需要等待Web服务中的事件完成,然后才能使用数据填充标签。我不确定是否有更好的方法?我正在寻找另一种管理方式。我将调用移到该类中以使代码简单。不知道这一切是否有意义 – Mike

回答

0

何时何地你叫一个getLabels方法?标签可能已经创建,但尚未初始化。

+0

是的,这是它....谢谢 – Mike

0

您可以使用这样的事情:

var element:Group = "searchId" in this ? this["searchId"] as Group : null;