views:

29

answers:

1

Hi!

Here is my code:

            var x : int = 50;
            var group : RadioButtonGroup = new RadioButtonGroup();
            for (var i : int = 0; i < 5; i++) {
                var rb : RadioButton = new RadioButton();
                rb.id = i.toString();
                rb.group = group;
                rb.label = i.toString();
                rb.x = x;
                x += 40;
                cnv_subContent.addElement(rb);//a BorderContainer
            }
            Alert.show(group.numRadioButtons.toString());

when i run the application, it shows me "0". Why this?

+3  A: 

This is due to the flex component lifecycle. When a RadioButton is assigned a group, it doesn't actually get added until its commitProperties runs a frame later.

To get the correct group.numRadioButtons, you'll have to do things asynchronously. Something interesting is spark.components.RadioButtonGroup actually dispatches an undocumented "numRadioButtonsChanged" event whenever radio buttons are added or removed. It works but of course being undocumented use at your own risk.

Dave