问题描述:

I have several TextInputs in a certain form which have been assigned id in an incremental order. For example:

<s:TextInput id = "index1"/>

<s:TextInput id = "index2"/>

<s:TextInput id = "index3"/>

Based on certain conditions I select the text from the corresponding TextInput and pass the value (index#.text) into a function

 foo(var index:String)

If I had just one of the TextInput I could have used:

foo(index1.text)

Can someone suggest how I can pass the textInput using its id.

Thank you.

-H

网友答案:

I plus one the request for 'certain conditions'. You post alludes to the fact that you can't access the component by name, so I'm writing the rest of this based on that assumption.

To access the values of a component and pass parameters of that component into a function you need an identifier, or link, to that component. It is easiest if you use the components name. But, that is not always possible. For example, the Flextras Calendar component creates, and displays, the days of the month. Depending what month is displayed, there may be 28, 30, or 31 days. It is not practical to access them by a unique name.

this is a similar situation in a ListBased class. You won't know, at compile time, how many itemRenderers you're going to need or have on the screen at one time.

One way to approach this is to have an array of the relevant objects (dayRenderers, itemRenderers, or in your case TextInputs). When doing processing you can loop over the array and process the element. Something like this:

for (var x = 0; x<objectArray.length; x++){
   foo(objectArray[x].text); 
}

If that is not desirable to you, for whatever reason, you can loop over the children of a container doing something like this:

for (var x = 0; x<container.numChildren; x++){
  var object : Object = this.getChildat(x);
  if(object is TextInput){ foo(object.text) }
}

Functional, but it can be a bit tedious at times. It really depends what you're trying accomplish.

相关阅读:
Top