Flex 3 - Issues passing parameters through an event -


I am stuck with an issue and I do not understand why this happens ....

In a loop, I am passing as an event parameter for the index. However, the event handler is getting a wrong index, but the correct target ...

Here is some code .AS class method:

  set the public function Data Agent (a: Array ): Zero {var x: int = 0; If (a! = Null & amp; amp; a.length & gt; 0) {label array = (new array collection (A)); Trace ("################################### ###############); (Var i: int = 0; i & lt; a.length; i ++) {var btn: NavigationRobotn Ctrl = new navigationArroboton Ctrl (); Var s: String = (New Array Collection (A)). GetItemAt (i) .toString (); Trace (i + '/' + s); Btn.addEventListener (FlexEvent.CREATION_COMPLETE, function (e: FlexEvent): zero {var lblIndex: int = i; btnAddLblHandler (e, lblIndex);}); This.addChild (btn); This.setChildIndex (BTN, 0); } Trace ("#################################) # ###############); }} Private function btnAddLblHandler (e: FlexEvent, ind: int): zero {trace (labelsArray.length.toString () + '/' + ind.toString ()); If (Ind and Lieutenius Array.Lampers) {Trace ('Handler' + IND + '/' + string (Labelsare.Get ITEM (Ind))); (E.target as navigation arrow button Ctrl) .lbl_body.text = string (labelsare.get istam (ind)); }}  

Detecting result here:

  ################## ########################################## 0/Terms/1 / & gt; Beads 2 /> EXTERIEUR 3 / & gt; InterAir 4 / & gt; Option 5 / & gt; Biodata ################################################# ############# 6/6 6/6 6/6 6/6 6/6 6/6  

In some way, handler is only happening The last value of the index 'i' is to get the current value of 'i' (0/1/2/3/4/5) in each loop ... any ideas what is wrong?

Thank you for your help! By that time the button is created and < Code> var lblIndex: int = i; For loopholes, the loop has completed its execution and the value of i will be a.length six here, you are either 5 different Create or redesign the functions.

  function getListener (i: number): function {return function (e: FlexEvent): zero {var lblIndex: int = i; BtnAddLblHandler (e, lblIndex); }; } Btn.addEventListener (FlexEvent.CREATION_COMPLETE, getListener (i));  

It looks like you are trying to set the label of navigationArbotcton item; It is an easy way to declare a [bindable] variable inside a square and pair it into the lbl_body.text field. If you are not going to update it later, then you do not really need to use the binding; You can just make it

The code will look like this: NavigationArrowButtonCtrl.mxml Text in public square: string; // Inside loop: btn.textvar = labelsArray.getItemAt (i) .toString (); Btn.addEventListener (FlexEvent.CREATION_COMPLETE, btnAddLblHandler) // btnAddLblHandler inside (e: FlexEvent): zero btn.lbl_body.text = btn.textvar; // You can do it yourself from the NavigationArrowButtonCtrl class. Lbl_body.text = this.textvar;

Instead of relying on e.target - use e.currentTarget . The target button may have some child - the current goal will always be an object with which the event listener was registered.

By the way, do not create the same array collection in each iteration of the loop:

Replace:

  var s: string = (new array collection a)). GetItemAt (i) .toString ();  

with

  var s: string = labels array.get ITEM (i) .toString ();  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -