Tintin
Tintin

Reputation: 2983

how could I traverse through children of a UIComponent Class in flex

I am dynamically creating a UI component based on XML templates stored in database. Now I want to access one of the child component of this UIComponent Class. I dont see any children property in this class? How could I possibly traverse through each child comppnent of this class and set its properties according to some logic in code(which I could not do in templates itself).

Basically what I want to do is something like this:

var cover:UICoomponent = generateScreen()
for each(var obj:UIComponent in cover.children){
if (obj.id =="myComponent"){
 obj.SomProperty = "SomeValue";
 }
}

Any help will be greatly appreciated. Thanks.

Upvotes: 0

Views: 2522

Answers (1)

Marty Pitt
Marty Pitt

Reputation: 29300

Use numChildren:

var cover:UICoomponent = generateScreen()
for (var i:int = 0; i < cover.numChildren; i++)
{
    var obj:UIComposer = cover.getChildAt(i) as UIComponent;
    if (obj.id =="myComponent") {
        obj.SomProperty = "SomeValue";
    }
}

Update: Traverse recursively

function findChild(childId:String,container:DisplayObjectContainer=null):UIComponent
{
   container ||= this;
   for (var i:int = 0; i < container.numChildren; i++)
   {
      var obj:UIComponent = container.getChildAt(i) as UIComponent;
      if (obj.id == childId)
         return obj;
      if (obj is DisplayObjectContainer)
      {
         // search for children
         child = findChild(childId,obj as DispalyObjectContainer);
         if (child)
            return child;
       }
    }
}

Usage:

var child:UIComponent = findChild("myComponent");
child.someProperty="SomeValue";

I haven't compiled this - but you get the general idea.

Upvotes: 3

Related Questions