I have written a few custom components in Flex 4, and run into this issue a few times.
var myForm:MyForm = new MyForm;
myForm.SetData(data);
addElement(myForm);
Now Imagine I am calling these functions from a non-constructor function of a Panel or VGroup (or any other container). Annoyingly, during MyForm.SetData(), not all fields of myForm that are declared there are yet initialized. Such as:
<s:VGroup id="dataGroup">
If my SetData()-function wants to access dataGroup (for the reason to .addElement() the just recieved data to it), it just fails with a nullpointer exception, because dataGroup is not yet created, despite this being after the constructor. How can guarantee that the form was initialized fully?
Listening for the
creationCompleteevent and adding your components in the handler for the event is one way to do this. This is what Sam DeHaan suggested.Another way you can do this is to override the
createChildren()function. This is the function that creates and adds all of a component’s child components. Code would look something like this:The docs on the component lifecycle will provide a ton of detail on this topic.