Pregunta

necesito nietos acceso de topbox y determinar si son o no los botones.

Esta función a continuación (gracias Flextras.com) agarra hijos inmediatos de topbox que son los hboxes. ¿Cómo hago un nivel inferior a los nietos de topbox? O es la única manera de hacer esto es ejecutar esta función en cada HBox?

for (var i : int =0; i<topBox.numChildren; i++){
  var child : DisplayObject = topBox.getChildAt(i);
  var myButton:UIComponent = child as UIComponent;
    blah blah               
} 


<mx:VBox id="topBox">

    <mx:HBox id="Hbox1">
        <mx:Button id="button1"
            label="button1" 
            click="myClickHandler"/>

        <mx:Button id="button2" 
            label="button2" 
           click="myClickHandler"/>
    </mx:HBox>

   <mx:HBox id="Hbox2">
        <mx:Button id="button3"
            label="button3" 
            click="myClickHandler"/>

        <mx:Button id="button4" 
            label="button4" 
           click="myClickHandler"/>
    </mx:HBox>

<mx:VBox>
¿Fue útil?

Solución

//declare child access function
private function processChildren(target:DisplayObjectContainer, handler:Function):void
{
    for (var i:int = target.numChildren - 1; i >= 0; i--)
    {
        handler(target.getChildAt(i));
    }
}

//call it with handler as anonymous function (may be plain one as well)
processChildren(topBox, function(target:DisplayObjectContainer):void
   {
       processChildren(target, function(target:DisplayObjectContainer):void
       {
           //this will be called on grandchildren
           if (target is Button)
           {
               //do something
           }
       });
   });

Para hacer que se vea menos intimidante, se puede reemplazar las funciones anónimas con las sencillas, pero la idea debe ser clara.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top