【问题标题】:Recursive iteration through every component instance通过每个组件实例进行递归迭代
【发布时间】:2009-11-17 19:48:17
【问题描述】:

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

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

【问题讨论】:

    标签: apache-flex actionscript-3 mxml


    【解决方案1】:

    这样就可以了:

    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;
    }
    

    【讨论】:

      【解决方案2】:

      我用它来迭代几种方法中的所有组件,其中一些方法在累加器acc 中建立结果(例如,写入一个字符串,记录所有的计数,等等)。当useRawtrue 时,迭代包括组件chrome。

          /**
           * 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);
          }
      

      【讨论】:

        【解决方案3】:

        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;
            }
        }
        

        【讨论】:

        • 我喜欢你的函数,但它似乎有一些语法错误。 UIComponent 没有 .getChildren() 函数。
        猜你喜欢
        • 2017-09-16
        • 1970-01-01
        • 2012-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-29
        • 1970-01-01
        • 2013-06-19
        相关资源
        最近更新 更多