views:

332

answers:

1

I am having troubles adding a radiobutton to a VBox in actionscript.

    var radioButton:RadioButton = new RadioButton();
    radioButton.groupName = "source";
    radioButton.label = "label";

    radioButton.selected = false;
    radioButton.addEventListener(Event.CHANGE, sourceChangeHandler);

    vBox.addChild(radioButton);

I first created these radiobuttons in mxml and it worked fine, but now that the radiobuttons need to be dynamically generated in actionscript it doesnt work.

When stepping through in the debugger I get to a binding error (1009) when trying to execute the addchild statement and nothing shows up in the VBox. Is there something I am doing wrong?

+1  A: 

this looks okay the only thing I can see is that you might try changing

radioButton = new RadioButton();

to

var radioButton:RadioButton = new RadioButton();

If it's not that it might have something to do with how your instantiating your VBox.

EDIT

As per our comments, it looks like the real problem is that the viewstack should have a property changed.

creationPolicy="All"

This way the vBox exists in memory when the radio button is being added to it.

invertedSpear
sorry about the confusion. I cut the code out from a bigger file and the variable (radiobutton) was instantiated correctly but not in the snippet i posted. Fixed the snippet. Sorry.
asawilliams
I figured as much, Looking into your error a bit it seems like a likely cause is that the VBox isn't done being created yet when you are trying to call the addChild method, can you give a snippet of instantiating the VBox? And also maybe show when this function is being called?
invertedSpear
the container that this is in, is in a viewstack, and it is not the selected container. do you think this is the source of the problem?
asawilliams
Yes, if the item hasn't been brought up in the viewstack then it doesn't exist yet. you can fix this by changing the viewstack and adding the property creationPolicy="All" to it. This will create all the items in your viewstack so they can have actions performed on them.
invertedSpear