Search code examples
apache-flexskin

Skin class is not getting initialized when I expect it to


Suppose that I have a chunk of code like this:

var myPopup:MyPopup = new MyPopup();    
myPopup.mainModel = model;      
PopUpManager.addPopUp(myPopup,this);    

The beginning of MyPopup looks like this:

<views:BlaBla 
    ...
    skinClass="com.mySkinClass" 
    ...
    >

    <fx:Script>
        <![CDATA[

            [SkinPart] public var aButton:Button;

            public function set mainModel(mainModel:Something):void {
                ...
                aButton.addEventListener(...);
                ...
            }

The mainModel setter references the aButton variable that is initialized in the skin. Oddly enough the skin is not initialized until after the the setter has run. This causes a null pointer exception. I expect the skin to be initialized in the var myPopup:MyPopup = new MyPopup(); line. Why is this not the case? Is there a way that I can force the skin to be initialized?


Solution

  • To answer my own question: the skin is not created until until the Object is added to the stage. This means that you either have to do this:

    Option 1

    var myPopup:MyPopup = new MyPopup();    
    PopUpManager.addPopUp(myPopup,this);  
    myPopup.mainModel = model;  
    

    or

    Option 2

    var myPopup:MyPopup = new MyPopup();  
    myPopup.mainModel = model;   
    PopUpManager.addPopUp(myPopup,this);  
    

    But move all references to objects created in the skin from the setter for mainModel and into a method triggered by creationComplete.