2013-12-17 3 views
0

런타임 중에 추가되는 자식 요소를 어떻게 참조 할 수 있습니까?자식 요소에 대한 액션 스크립트 참조

예제 코드 : 나는 this["path1"].text = "new path";를 호출하여 라벨의 디렉토리 경로를 변경하려고하고 오류 내가 그 자식 요소에 참조 할 수있는 방법

Error #1069: Property path1 not found and there is no default value. 

로 실행

for(var i:Number=1; i<=5;i++){ 
    var mygroup:HGroup = new HGroup(); 

    var mybutton:Button = new Button(); 
    mybutton.label = "Browse Directory"; 
    mybutton.addEventListener(MouseEvent.CLICK,browsedirectory); 

    var dirlbl:Label = new Label(); 
    dirlbl.text = "C:/some directory/"; 
    dirlbl.id = "path"+i; 

    mygroup.addElement(mybutton); 
    mygroup.addElement(dirlbl); 
    mygroup.id = "group"+i; 
    mainGroup.addElement(mygroup); 
} 

public function updatepath():void 
{ 
    this["path1"].text = "new path"; 
} 

?

감사합니다.

+0

"this"는 코드에서 무엇을 나타 냅니까? –

+0

'this'는 루트에 있습니다. – user1995781

+0

다음 링크를 확인하십시오. [link1] (http://www.daveoncode.com/2009/05/20/objectcollector-accessing-dynamic-generated-flex-objects-by-id/) [link2] (http : // www .jumpingbean.co.za/blogs/mark/flex_reference_components_dynamic_runtime_creation) –

답변

1
Caution, code not tested! 
먼저 (자신의 지식에 대한) 특정 컨테이너의 모든 요소를 ​​확인할 수 있습니다

:

private function displayChildren(element):void { 
    for (var i = 0; i < element.numChildren; i++){ 
     var child = element.getChildAt(i); 

     if (child != null) { 
      trace ('position: '+i+' child name:' + child.name); 

      for (var j = 0; j < child.numChildren; j++){ 
       displayChildren(child.getChildAt(j)); 
      } 
     } 
    } 
} 

displayChildren(mainGroup); 

등이 출력해야 뭔가 :

trace= position: 0 child name : "group1";  <- HGroup 
trace= position: 0 child name : undefined; <- Button without id 
trace= position: 1 child name : "path1";  <- Label 
... 

그래서이 다음, 당신을 다음과 같은 방식으로 라벨에 액세스 할 수 있어야합니다.

-------------------- 업데이트 -------------------------- -

다음과 같이하면 어떻게됩니까?

var vgroup = mainGroup.getChildByName("group1"); 
if (vgroup != null) { 
    trace(vgroup.id) 
    var label = vgroup.getChildByName("path1"); 
    if (label != null) { 
     trace(label.id) 
     label.text = "change label text" 
    } 
} 

출력이 있습니까?

+0

답변 해 주셔서 감사합니다. 모든 요소는 컨테이너에 있습니다. 그러나 당신의 예제를 참고하면 오류 # 1069 : spark.components.VGroup에 group1 속성이없고 기본값이 없습니다. – user1995781

+0

업데이트 된 답변보기. –

+0

@ user1995781 - 해결책이 효과가있는 경우 - 답변을 수락하거나 더 자세한 정보가 필요하면 알려주십시오. –