1
votes

In my main application I have a viewstack with 3 child views. In the viewstack change handler, I programmatically change the selectedchild property.

I understand that the initialize method for the view is not called every time I change the selectedChild Property. So I tried to invoke the init method programmatically too..

view1.mxml

    <fx:Script>
    <![CDATA[
    public function init():void{
     //something        
    }
    ]]>
    </fx:Script>

main.mxml

viewStack.selectedChild = viewStack.getChildByName("viewname") as NavigatorContent;
var v1:view1 = new view1();
v1.init();

But I get a null pointer error. Am I missing anything? Any help would be appreciated. I am a beginner here.

2
I'm a little confused on what you are trying to accomplish. If you want to call a method in the MXML you can do something like change="{myfunction()}" . You shouldn't need to programatically change the select child property on a change handler as Flex should already know about that. - Collin White
the function is not implemented in the same file. The function is implemented in the mxml component(view1.mxml) and I am trying to access the function in the main application(main.mxml). - Pavithra Jayakumar

2 Answers

0
votes

In your main.mxml app, you are creating a new instance of the view1 component and what you need is to execute the init() method of the current instance of the viewstack.

Why don't you try doing somethig like this:

var view:View1 = viewStack.getChildByName("viewname").getChildByName("yourComponentId") as View1;

view.init();

where yourComponentId is your component inside the navigatorContent inside your Viewstack.

Anyway you should't be doing this, at least not this way.

Greetings!

Gabriel.-

0
votes

I used

FlexGlobals.topLevelApplication.[viewId].init();

And it works!!