每个组件实例的递归迭代

时间:2009-11-17 19:48:18

标签: flex actionscript-3 mxml

我的flex应用程序中有各种自定义组件实例。我想以递归方式遍历它们并获取它们的实例ID。递归部分对我来说非常重要。 有人能告诉我这是最好的方法吗? 我试过这样做,但它没有做递归:

for each (var myItem:* in this.MasterContainer.childDescriptors)
{
   trace(myItem.id);
}

3 个答案:

答案 0 :(得分:4)

这样做:

private function findControlsRecursive(current:DisplayObjectContainer, controls:ArrayCollection):void
{
    for(var idx:int = 0; idx < current.numChildren; idx++)
    {
        var child:DisplayObject = current.getChildAt(idx);
        controls.addItem(child.name);
        var childContainer:DisplayObjectContainer = child as DisplayObjectContainer;
        if(childContainer)
        {
            findControlsRecursive(childContainer, controls);
        }
    } 
}

public function findControls():ArrayCollection
{
    var controls:ArrayCollection = new ArrayCollection();
    findControlsRecursive(Application.application as DisplayObjectContainer, controls);
    return controls;
}

答案 1 :(得分:2)

我使用它来迭代几个方法中的所有组件,一些方法在累加器acc中构建结果(例如,写入一个字符串,保持所有的计数,无论如何)。 useRawtrue时,迭代包含组件镶边。

    /**
     * Descends through subcomponents applying function
     */
    public static function visitComponent(component:Object, fn:Function, useRaw:Boolean = false, acc:Object = null):Object {
        var newAcc:Object = fn.call(null, component, acc);

        if (component is mx.core.Container) {
            var kids:mx.core.Container = mx.core.Container(component);
            var childList:IChildList;
            if (useRaw) {
                childList = kids.rawChildren;
            } else {
                childList = kids;
            }
            for (var i:int = 0; i < childList.numChildren; ++i) {
                visitComponent(childList.getChildAt(i), fn, useRaw, newAcc);
            }
        } else if (component is DisplayObjectContainer) {
            var displayObjContainer:DisplayObjectContainer = component as DisplayObjectContainer;
            for (var j:int = 0; j < displayObjContainer.numChildren; ++j) {
                visitComponent(displayObjContainer.getChildAt(j), fn, useRaw, newAcc);
            }               
        }
        return newAcc;
    }

    /**
     * Randomly resets backgroundColor of subcomponents
     */
    public static function colorizeComponent(component:Object):void {
        var fn:Function = function(c:Object, acc:Object):Object {
            if (c is UIComponent) {
                (c as UIComponent).setStyle("backgroundColor", Math.random() * uint.MAX_VALUE);
                (c as UIComponent).setStyle("backgroundAlpha", 0.2);
            }
            return null;
        }

        visitComponent(component, fn);
    }

答案 2 :(得分:1)

Michael Brewer-Davis的功能比这个例子(处理rawchildren,非flex组件等)更多,但是不需要递归来遍历树:

function toEveryComponentDo(f:Function):void
{
    var component:UIComponent = Application.application;
    var components:Array = component.getChildren();
    f(component);

    while (components.length > 0)
    {
        var childComponents:Array = [];

        for each (component in components)
        {
            f(component);
            if (component is Container)
            {
                childComponents = childComponents.append(Container(component).getChildren());
            }
        }

        components = childComponents;
    }
}