Looking for a more efficient way to clear controls
In main.mxml I have a bunch of textInput controls a combobox and a few checkboxes that I would like to be able to clear with some sort of loop. Right now, I开发者_JAVA技巧 do it like this:
public function clearAll():void
{
customerIDInput.text = "";
listIDCombo.selectedItem = "";
listVersionInput.text = "";
suppressMasterFilesInput.text = "";
priorOrderSuppressInput.text = "";
onePerSelectInput.text = "";
geoCountOptionsInput.text = "";
keyCodeInput.text = "";
nthSelectInput.text = "";
geoTypeInput.text = "";
geoValueFromInput.text = "";
latitudeInput.text = "";
longitudeInput.text = "";
begRadiusInput.text = "";
endRadiusInput.text = "";
geoSelectOmitCheck.selected = false;
fieldIDInput.text = "";
fieldValueInput.text = "";
fieldSelectOmitCheck.selected = false;
outputFieldCheck.selected = false;
}
I read a post on SO that recomended adding the controls to an ArrayCollection with the creationComplete event. I tried that and it worked fine but it was not any more elegant than what I have now. All of these controls are in mxml format and not generated with AS by me. I also tried looping like this:
for each (var ctrl:UIComponent in Application)
{
switch(ctrl.className)
{
case TextInput:
I can't get past that part though. I cannot find a way to reference the values of the control. Anyone know?
I'd probably do it the way you do it. Is there a compelling reason not to do it this way? Given the naming conventions, I am guessing you have an explicit number of defined controls at compile time. It does not appear that you have an unknown number.
You could work out a loop for all the children of the component:
for (var index:int=0;this.numChildren ; x++){
var component : UIComponent = this.getChildAt(index) as UIComponent;
if(component is TextInput){
component.text = '';
} else if (component is ListBase){
component.selectedIndex = null;
}
// etc for other comp types
}
But, it seems like you're adding undue processing; making your code harder to develop; and harder to read.
精彩评论