2011-08-06 59 views
0

我是根據從XML文件獲得的信息動態創建面板,但是我在向這些面板添加按鈕時遇到問題。這些buttoms也是基於從XML文件中獲取的信息創建的。問題似乎是我給面板一個Id名稱的方式。任何你能給的幫助都會很棒。將按鈕添加到動態創建的面板

private function sidebar():void{ 
      for each (value in [email protected]) 
      { 
       var myInstance4:spark.components.Panel = new spark.components.Panel(); 
       myInstance4.title = value; 
       myInstance4.id = value; 
       sidebarbox.addChild(myInstance4); 
       Alert.show(myInstance4.id) 
       for each (value2 in [email protected]) 
       { 
        var myInstance3:spark.components.Button = new spark.components.Button(); 
        myInstance3.label = value2; 
        myInstance3.addEventListener("click",changeIt); 
        myInstance3.id=value2;  
      //  value.addChild(myInstance3); 
       // value.addElement(myInstance3); 
       } 

      } 
     } 

回答

0

使用myInstance4.addElement添加孩子。

您已經通過調用

var myInstance4:spark.components.Panel = new spark.components.Panel(); 

如果創建的MXML這些元素創建一個實例,是的,你會使用id屬性,當你在ActionScript使用它來唯一標識每個對象。例如

<s:Button id="myButton" label="My Button" /> 

protected function creationCompleteEvent(event:FlexEvent):void 
{ 
    myButton.doSomething(); 
} 

,但

如果您使用ActionScript創建它,您無需設置id屬性,只要你有對象的引用來訪問它。

protected function creationCompleteEvent(event:FlexEvent):void 
{ 
    var myNewButton:Button = new Button(); 
    //You do not need to set the id here 
    myNewButton.doWhatEver(); 
} 
相關問題