My understanding is that id's get converted into public variables. Probably the
easiest way to do what you are trying to do here is to set up a variable in the
ActionScript, and assign your new Text object to it. Then you can just use that
variable when you want to access the object. With this approach, the first part
of your script would look like this:
import mx.controls.Text;
private var myChild:Text;
private function addMyChild():void {
myChild = new Text();
myChild.text = "Hello World!!";
myChild.id = "kid1";
myChild.name = "kid1";
myChild.uid = "kid1";
myPanel.addChild(myChild);
}
If you need to have references to multiple components, you could store them in
an array.
----- Original Message ----
From: Thatcher <[EMAIL PROTECTED]>
To: [email protected]
Sent: Wednesday, July 30, 2008 12:18:29 PM
Subject: [flexcoders] Interesting problem: dynamically creating id's and later
referencing them.
Hi Guys (and Girls),
Ok, so I have been fighting a couple of days to figure this out.
In order to make a large and compliated form with drag and drop in of
more form fields I want to dynamically add an id to an display
object, and then be able to reference it later to get data out.
Somehow it only seems possible to find these objects by navigating
trough the the displaylist, and I cannot assign any 'real' id to a
newly created child.
The following example should illustrate what I want to do (it should
run). You will find it quite imposssible to get the object by it's id
(or uid).
Why is that?? Is there any other way of referencing an object of
which you do not know who his parent is?
Thank you in advance and greetings,
Thatcher
<?xml version="1.0" encoding="utf- 8"?>
<mx:Application xmlns:mx="http://www.adobe. com/2006/ mxml"
layout="vertical" >
<mx:Script>
<![CDATA[
import mx.controls. Text;
private function addMyChild() :void {
var myChild:Text = new Text();
myChild.text = "Hello World!!";
myChild.id = "kid1";
myChild.name = "kid1";
myChild.uid = "kid1";
myPanel.addChild( myChild);
}
private function getMyChildById( ):void {
var childReference: Text = this["kid1"] as Text;
trace (childReference) ;
}
private function getMyChildByName( ):void {
var childReference: Text = myPanel.getChildByN ame
("kid1") as Text;
trace (childReference) ;
}
]]>
</mx:Script>
<mx:Button label="add my child" click="addMyChild( )" />
<mx:Panel id="myPanel" ></mx:Panel>
<mx:Button label="get my child by id" click="getMyChildBy Id()" />
<mx:Button label="get my child by name" click="getMyChildBy Name()" />
</mx:Application>