5 Replies Latest reply on Jan 24, 2008 10:58 AM by m_hartnett

    How to detect event in View Stack?

    Handycam Level 1
      Previously in my application, when I have constructed a viewStack I have used its hide and show events to trigger code, as in:

      <comp:Step3 id="s3" hide="s3.exitStep()" show="s3.init(),showPanel()" />

      However, I am adding items dynamically to the view stack:

      var _step4:Step4 = new Step4();
      mainViewStack.addChild(_step4);
      _step4.label = "Choose Your Finishing Touches"

      How do I set the hide and show event for a vs child created this way?

        • 1. How to detect event in View Stack?
          m_hartnett Level 3
          You can add event listeners to the vs.

          _step4.addEventListener( FlexEvent.HIDE,hideFunctionName)
          _step4.addEventListener( FlexEvent.SHOW,showFunctionName)

          The method (hideFunctionName) will accept an event object. (event.currentTarget)
          That object will be the vs. From there you can execute the specific vs methods.
          • 2. Re: How to detect event in View Stack?
            Handycam Level 1
            Thanks.

            Will this work even if the function in question is IN the component, not the main application, or do I have to do something special to target it?
            • 3. Re: How to detect event in View Stack?
              m_hartnett Level 3
              yes, however the method that is defined as the handler must accept an event object like exitSetup( e : Event).

              _step4.addEventListener( FlexEvent.HIDE,s3.exitStep)

              You should also be aware that if you remove a vs using removeChild method you should also remove the listener.

              this is because Flex garbage collection will not destroy the object if there is a listener still attatched. This may not cause issues instantly but by not removing the listeners it is taking up memory and you could run into problems later.
              • 4. Re: How to detect event in View Stack?
                Handycam Level 1
                Thanks this works except for the first child -- apparently the "show" event is not triggered since the new child immediately replaces the removed prior children.

                I can get it to work if I trigger the enterStep function on creationComplete, but then it won't work right on subsequent shows.

                So I tried adding a line to trigger it after creation, but of course there's an argument mismatch since my function requires an event to be passed to it (as per your previous post).

                My function in the main app:

                private function changeStack(_which:int) : void {
                mainViewStack.removeChild(s0);
                mainViewStack.removeChild(s1);
                if (_which == 1){
                var _step2:Step2 = new Step2();
                mainViewStack.addChild(_step2);
                _step2.label = "Choose Cake Flavor"
                _step2.addEventListener( FlexEvent.HIDE,_step2.exitStep) ;
                _step2.addEventListener( FlexEvent.SHOW,_step2.enterStep) ;
                _step2.enterStep();
                var _step3:Step3 = new Step3();
                mainViewStack.addChild(_step3);
                _step3.label = "Choose Frosting Flavor"
                _step3.addEventListener( FlexEvent.HIDE,_step3.exitStep) ;
                _step3.addEventListener( FlexEvent.SHOW,_step3.enterStep) ;
                var _step4:Step4 = new Step4();
                mainViewStack.addChild(_step4);
                _step4.label = "Choose Your Finishing Touches"
                _step4.addEventListener( FlexEvent.HIDE,_step4.exitStep) ;
                _step4.addEventListener( FlexEvent.SHOW,_step4.enterStep) ;
                }
                }

                The line _step2.enterStep(); is not working, no event.

                • 5. Re: How to detect event in View Stack?
                  m_hartnett Level 3
                  As long as you are not using the event object in your method you can pass in a null and it will work for your initial setup.

                  The event object will contain the object that is listening for the event like event.currentTarget. The currentTarget would be the vs.

                  If you are not using any of the properties of the event object then you do not need to worry about passing a real event to the listener.

                  If all of your hide and show methods do the same thing to the vs and your listener was external to the vs then the event.currentTarget object would be of use to you.

                  For example, if you add a listener that is defined in a container that is hosting the vs then you could define the following.

                  _step4.addEventListener( FlexEvent.HIDE,container.exitStep) ;
                  _step4.addEventListener( FlexEvent.SHOW,container.enterStep) ;

                  private function exitStep( e : Event ) : void {
                  var vs : ViewStack = e.currentTarget as ViewStack;
                  vs.doSomething
                  }

                  If all of your vs's do the samething during exitStep this would work. If you needed special processing for each specific vs then adding the listener to the vs would probably be a better approach.