2 Replies Latest reply on Aug 12, 2009 12:52 PM by jherr22

    ViewStack- some pages call functions, others do not

    jherr22

      I have one file mxml project.  It has a Viewstack with 2 views (page 1 and page 2).

      Page 1 has 5 chkboxes and 5 buttons.

      You click on a chkbox 1, and it calls a function that disables button 1.

      Then you goto Page 2, which simply shows the same 5 buttons, with button 1 disabled.

       

      Now go back to Page 1, and click on chkbox 2,  which disables button 2.

      Next, goto Page 2, and you will see button 1 disabled, but button 2 is still enabled.

       

      I keep going back and forth between Page 1 and Page 2, clicking the chkboxes,

      Page 1 keeps track of which ones have been disabled, but Page 2 only remembers the disabling of button 1.

       

      Using trace(), I found that the function that does the disabling on the re-display of Page 2, only gets called upon the first display of Page 2.

       

      I have tried many different approaches, including the variations of 'creationPolicy', and nothing has worked.

      How can I get Page 2 to call the function on the re-displays??

      The entire code follows, if needed.

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application

       

      xmlns:mx="http://www.adobe.com/2006/mxml

      "

      layout="

       

      vertical"

      >

      <mx:Script>

      <![CDATA[

       

       

      public var buttonStates:Array = [true,true,true,true,true

      ];

       

       

      //------------------------------------------

       

       

      public function

      getState(ndx:int):Boolean

      {

       

       

      return

      buttonStates[ndx];

      }

       

       

      //------------------------------------------

       

       

      public function setFalse(ndx:int):

      void

      {

       

       

      //disable the displayed button right now

       

       

      var mybtn:String = "pg1Btn"

      +String(ndx);

       

       

      this[mybtn].enabled = false

      ;

       

       

      //for future display (on Page 2)

      buttonStates[ndx] =

       

      false

      ;

      }

       

       

      //------------------------------------------

       

       

      public function goPage1():

      void

      { pageStack.selectedIndex = 0; }

       

      //page 1

       

       

      //------------------------------------------

       

       

      public function goPage2():

      void

      { pageStack.selectedIndex = 1; }

       

      //page 2

       

       

      //------------------------------------------

      ]]>

      </mx:Script>

       

      <mx:ViewStack

       

      id="pageStack" width="100%" height="100%" >

       

      <!-- PAGE 1 -->

      <mx:VBox

       

      id="page1" width="100%"

      >

       

       

      <mx:VBox id="pg1header" width="100%">

       

       

      <mx:Text text="Page 1 - Click only ONE checkbox" fontSize="30" />

       

       

      </mx:VBox>

       

       

       

      <mx:HBox id="pg1boxes" width="100%"

      >

       

       

      <mx:VBox id="pg1chkboxes" fontSize="16"

      >

       

       

      <mx:CheckBox id="chkbox0" label="ChkBox 0" click="setFalse(0)"

      />

       

       

      <mx:CheckBox id="chkbox1" label="ChkBox 1" click="setFalse(1)"

      />

       

       

      <mx:CheckBox id="chkbox2" label="ChkBox 2" click="setFalse(2)"

      />

       

       

      <mx:CheckBox id="chkbox3" label="ChkBox 3" click="setFalse(3)"

      />

       

       

      <mx:CheckBox id="chkbox4" label="ChkBox 4" click="setFalse(4)"

      />

       

       

      </mx:VBox>

       

       

       

      <mx:VBox id="pg1buttons" fontSize="16" >

       

       

      <mx:Button id="pg1Btn0" label="Button 0" enabled="{getState(0)}"

      />

       

       

      <mx:Button id="pg1Btn1" label="Button 1" enabled="{getState(1)}"

      />

       

       

      <mx:Button id="pg1Btn2" label="Button 2" enabled="{getState(2)}"

      />

       

       

      <mx:Button id="pg1Btn3" label="Button 3" enabled="{getState(3)}"

      />

       

       

      <mx:Button id="pg1Btn4" label="Button 4" enabled="{getState(4)}"

      />

       

       

      </mx:VBox>

       

       

      </mx:HBox>

       

       

      <mx:VBox>

       

       

      <mx:Button id="pg2Btnxfer" label="Go To Page 2" fontSize="24

      "

      click="{goPage2()}"

       

      />

       

       

      </mx:VBox>

      </mx:VBox>

       

      <!-- PAGE 2 -->

      <mx:VBox

       

      id="page2" width="100%"

      >

       

       

      <mx:VBox id="pg2header" width="100%" >

       

       

      <mx:Text text="Page 2 - Did the button go inactive?" fontSize="30"

      />

       

       

      </mx:VBox>

       

       

       

      <mx:HBox id="pg2hbox" width="100%"

      >

       

       

      <mx:VBox id="pg2vbox1" width="50%"

      >

       

       

      <mx:Button id="pg2button" label="Go Back to Page 1" fontSize="24

      "

      click="goPage1()"

       

      />

       

       

      </mx:VBox>

       

       

       

      <mx:VBox id="pg2vbox2" width="25%" >

       

       

      <mx:Button id="pg3Btn0" label="Button 0" enabled="{getState(0)}"

      />

       

       

      <mx:Button id="pg3Btn1" label="Button 1" enabled="{getState(1)}"

      />

       

       

      <mx:Button id="pg3Btn2" label="Button 2" enabled="{getState(2)}"

      />

       

       

      <mx:Button id="pg3Btn3" label="Button 3" enabled="{getState(3)}"

      />

       

       

      <mx:Button id="pg3Btn4" label="Button 4" enabled="{getState(4)}"

      />

       

       

      </mx:VBox>

       

       

      </mx:HBox>

      </mx:VBox>

       

      </mx:ViewStack>

       

       

      </mx:Application>

        • 1. Re: ViewStack- some pages call functions, others do not
          _Natasha_ Level 4

          Hi,

          now it works as it written.

          enabled property for each button on Page2 called on creation. Changing active page doesn't coursed deleting any components and recreating after switching back.

          Binding is a very useful thing gor this snd it's also easier.

          The first change:

          [Bindable]

          public var buttonStates:ArrayCollection = new ArrayCollection([true,true,true,true,true]);

           

          ArrayCollection can be used in bindings, Array - not.

          After that bind enabled propery to data from ArrayCollection. Also you can bind buttons from Page1 like this and delete string

          this[mybtn].enabled = false; from public function setFalse(ndx:int)

          :

           

          <mx:Button id="pg3Btn0" label="Button 0" enabled="{buttonStates.getItemAt(0)}"

          />

           

           

          <mx:Button id="pg3Btn1" label="Button 1" enabled="{buttonStates.getItemAt(1)}"

          />

           

           

          <mx:Button id="pg3Btn2" label="Button 2" enabled="{buttonStates.getItemAt(2)}"

          />

           

           

          <mx:Button id="pg3Btn3" label="Button 3" enabled="{buttonStates.getItemAt(3)}"

          />

           

           

          <mx:Button id="pg3Btn4" label="Button 4" enabled="{buttonStates.getItemAt(4)}"

          />

           

          • 2. Re: ViewStack- some pages call functions, others do not
            jherr22 Level 1

            That WORKED !!!

            Thank you

            Thank you

            Thank you

            --- john