Are you asking about:

 

private function fAddCustomView(compClass:Class):void
{
var canCustView:Container = Container(new compClass());
canCustView.label = "Custom " + 
cbCompList.selectedItem.label;
vsComps.addChild(canCustView);
}



...

fAddCustomView(Comp1);

 

 

________________________________

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of phall121
Sent: Monday, June 04, 2007 12:39 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Selecting which Child Components to add at runtime

 

I want to add views to a ViewStack at runtime by selecting from an 
expanding table of Custom Components.

As the simplified code below shows, I can figure out how to add any 
one and hard code it. But the addChild method of the ViewStack 
seems to require that you know ahead of time which component you 
want to add. I can not find in the docs or online a way to make 
this dynamic.

In other words, when I define the new component view to add to the 
view stack.... 
{
var canCustView:Comp1 = new Comp1();
canCustView.label = "Custom " + cbCompList.selectedItem.label;
vsComps.addChild(canCustView);
}
...I want to choose at runtime whether to instantiate views from 
Comp1, Comp17, or Comp53, etc.

Any ideas how to accomplish this? Thanks for your thoughts!

Here is the simplified code. 

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> " 
layout="absolute"
xmlns:comp="components.*">

<mx:Script>
<![CDATA[
import components.*;

private function fAddView():void
{
var canHardView:TestComp = new TestComp();
canHardView.label = "Hard-Code View2";
vsComps.addChild(canHardView);
}

private function fAddCustomView():void
{
var canCustView:Comp1 = new Comp1();
canCustView.label = "Custom " + 
cbCompList.selectedItem.label;
vsComps.addChild(canCustView);
}

]]>
</mx:Script>

<mx:Button x="25" y="36" label="Add Hard-Coded View"
click="fAddView()"/>

<mx:Button x="25" y="68" label="Add Custom Component"
click="fAddCustomView()"/>

<mx:ComboBox id="cbCompList" x="203" y="67">
<mx:dataProvider>
<mx:Array id="arrCompList">
<mx:Object label="Component1" data="Comp1"/>
<mx:Object label="Component2" data="Comp2"/>
<mx:Object label="Component3" data="Comp3"/>
<mx:Object label="Component4" data="Comp4"/>
<mx:Object label="Component5" data="Comp5"/>
</mx:Array>
</mx:dataProvider>
</mx:ComboBox>

<mx:TabBar direction="vertical" dataProvider="{vsComps}" 
labelField="app" y="108" x="27"/>

<mx:ViewStack id="vsComps" width="50%" height="50%"
x="200" y="108" backgroundColor="#FFFFC9">

<mx:Canvas id="vView1" label="View1" verticalScrollPolicy="off"
horizontalScrollPolicy="off">
<mx:Label x="68" y="2" width="130" text="Initial View -- 
View 1"/>
<mx:Label id="lblViewName" x="68" y="32" width="68" 
text="Input:"/>
<mx:TextInput id="txtiInput" x="136" y="32"/>
</mx:Canvas>
</mx:ViewStack>

</mx:Application>

 

Reply via email to