• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

Flex : Problem with instantiating an mxml component

I have two mxml files in a flex project: But when I trace a.cBtn, it is null. Why should it be?

test.mxml :
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="runIt()">
<mx:Script>
    <![CDATA[
        public function runIt():void
        {
            var a:abc = new abc();
            trace(a.cBtn);//a.cBtn is null here
        }
    ]]>
</mx:Script>    
</mx:Application>

And abc.mxml :
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
    <mx:Button x="108" y="73" label="Button" id="cBtn"/>

</mx:Canvas>
0
dtivmk
Asked:
dtivmk
  • 2
1 Solution
 
vindys80Commented:
Hi,

Component will be initiated only when you addChild the component into your container.

Thanks,
Vindys
var a:abc = new abc();
				addChild(a);
				trace(a.cBtn);

Open in new window

0
 
dtivmkAuthor Commented:
Is there any other way to get it done?
I have some reasons, for which I wouldn't like
to use addChild.
0
 
vindys80Commented:
Hi,

Until createComplete is called in your abc, you won't be able to get cBtn. So it gets called when you add it to your container. Another option will be to add a listener for your abc creation complete and deal with cBtn. I don't feel there is any other option.

Thanks,
Vindys
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now