Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding descendent children by name in flex
    primarykey
    data
    text
    <p>I am puzzled by what I am seeing here in this code below. I have a box container that has a child button (whose name I have specified). I have written a function that attempts to find the child button by name. However, this does not work as expected - The reason being the Box has numChildren=0 for some reason and I expect it to be 1 because I have a button added to it as a child. Can someone help me understand what I am doing wrong?</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"&gt; &lt;fx:Declarations&gt; &lt;!-- Place non-visual elements (e.g., services, value objects) here --&gt; &lt;/fx:Declarations&gt; &lt;mx:Box height="100%" width="100%" initialize="initializeApp();" name="MyBox"&gt; &lt;fx:Script&gt; &lt;![CDATA[ import mx.controls.Alert; import mx.controls.Button; import mx.core.FlexGlobals; public function initializeApp():void { var btn:Button = new Button(); btn.name = "MyButton"; addElement(btn); btn.addEventListener(MouseEvent.CLICK, clickCallback); } private function clickCallback(event:MouseEvent):void{ var obj:DisplayObject = findChildByName(FlexGlobals.topLevelApplication as DisplayObjectContainer, "MyButton"); if (obj==null){ Alert.show( "Not Found"); } else{ Alert.show( "Found"); } } private function findChildByName(parent:DisplayObjectContainer, name:String):DisplayObject{ var childCount:Number = (parent==null) ? 0 : parent.numChildren; for (var i:Number=0;i&lt;childCount;i++){ var child:DisplayObject = parent.getChildAt(i); if (child is DisplayObjectContainer){ return findChildByName(child as DisplayObjectContainer, name); } else{ if (parent!=null &amp;&amp; child == parent.getChildByName(name)){ return child; } } } return null; } ]]&gt; &lt;/fx:Script&gt; &lt;/mx:Box&gt; &lt;/s:WindowedApplication&gt; </code></pre> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload