What is special about the 'id' attribute on an MXML tag is that it does
some compile-time magic. Whenever you write an MXML file, you are really
writing an ActionScript class. If, for example, when you write

 

    MyApp.mxml:

 

    <Application>

        <Text id="kid1"/>

    </Application>

 

then it produces a class that looks like

 

    class MyApp extends Application

    {

        public var kid1:Text

        ...

    }

 

plus other stuff that causes the Text instance to get created and a
reference to it placed in the 'kid1' reference variable. So when you
specify id="kid1", you don't just get a property on the Text instance
named "id" whose value is the String "kid1"; you also get a property on
MyApp named "kid1" whose value is a reference to the Text instance!

 

When you assign to the "id" property at runtime, the second part can't
happen, because MyApp is a non-dynamic class and new properties like
kid1 can't get added to it at runtime.

 

The solution to your problem is to simply declare the reference variable
yourself. Declare

 

    public var kid1:Text;

 

as an instance variable in your <Script>, rather than making it a local
variable in the addMyChild() method. Then you can refer to kid1 anywhere
in your script.

 

If you don't know at compile time how many dynamic instances you'll be
creating at runtime, use an Array, an Object (as a collection of
name/value pairs), or a Dictionary to store references to the dynamic
instances that you create.

 

Gordon Smith

Adobe Flex SDK Team

 

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Thatcher
Sent: Wednesday, July 30, 2008 12:18 PM
To: [email protected]
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
<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.getChildByName
("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="getMyChildById()" />
<mx:Button label="get my child by name" click="getMyChildByName()" />

</mx:Application>

 

Reply via email to