1 Reply Latest reply on Oct 14, 2009 10:25 PM by Subeesh Arakkan

    How to pass a value to a component

    SiHoop Level 1

      In the following file I want to click on a button and send a value to a component. I want that value to change for different buttons but I cannot use the instance name to send that information and I also cannot use a method of tIe class. I tried

      instructorView.myVar= e.currentTarget.data 

       

      and

       

      instructorView.test()

       

      but both fail. How do I access the properties and methods for this component from the function named click()?

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:local="*" layout="vertical"  applicationComplete="init()">
          <mx:Script>
              <![CDATA[
          private var myData:Array=["first","second", "third"];
          private function init():void{
              for(var i:int=0;i<3;i++){
                  var button:Button=new Button;
                  button.data=myData[i];
                  mainMenu.addChild(button);
                  button.addEventListener(MouseEvent.CLICK, click);
              }
          }
          public function click(e:Event):void {
              trace("selected data="+e.currentTarget.data)
              viewStack.selectedChild=instructor 
              //instructorView.myVar= e.currentTarget.data            
              //instructorView.test()           
          }
              ]]>
          </mx:Script>
             <mx:Panel  layout="vertical" width="300" height="300"  >        
             <mx:ViewStack   id="viewStack" >
                  <mx:VBox id="mainMenu" label="Login" >
                  </mx:VBox>
                  <mx:VBox id="instructor" label="Instructor" >
                      <local:InstructorView  id="instructorView" />
                  </mx:VBox>
              </mx:ViewStack >
               <mx:HBox>
                   <mx:Button label="Main Menu" click="viewStack.selectedChild=mainMenu;"/>
               </mx:HBox>         
          </mx:Panel>
      </mx:Application>

       

      InstructorView.mxml

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"  creationComplete="init()" width="100%" height="100%">
      <mx:Script>
          <![CDATA[
      [Bindable] public var myVar:String=""
      public function init():void{
          trace("init"+myVar)
      }
      public function test():void{
          trace("test="+myVar)
      }
          ]]>
      </mx:Script>
      </mx:Canvas>