Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
863 views
in Technique[技术] by (71.8m points)

apache flex - Unable to retrieve property in main from child popup using event in FlashBuilder

I am using a component to display the popup and using an event listener to get popover properties and remove the popup in the Parent. The poup var, however, in the listeners popup var is nul so it throws an error.

Any suggestions would be greatly appreciated.

John


Here is my EditStudentLogInForm.mxml component..

<?xml version="1.0"?>
<!-- containerslayoutsmyComponentsMyLoginForm.mxml -->
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="handleCreationComplete();">
    <mx:Script>
        <![CDATA[
            import mx.managers.PopUpManager;
            [Bindable] public var studentLoginEmail:String;
        ]]>
    </mx:Script>

    <mx:Form width="333">
        <mx:FormItem label="Email">
            <mx:TextInput id="username" width="207"/>
        </mx:FormItem> 
        <mx:FormItem label="Password">
            <mx:TextInput id="password" 
                      width="205"/>
        </mx:FormItem> 
    </mx:Form>
    <mx:HBox> 
        <mx:Button id="okButton" label="OK"/> 
        <mx:Button id="cancelButton" label="Cancel" />
    </mx:HBox> 
 </mx:TitleWindow>

Here is the Parent...

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            xmlns:s="library://ns.adobe.com/flex/spark">    
    <mx:Script>
        <![CDATA[
            import flash.events.Event;
            import mx.managers.PopUpManager;
            import mx.core.IFlexDisplayObject;
            import EditStudentLogInForm;
            import mx.containers.TitleWindow;

            public var helpWindow:EditStudentLogInForm;

            public function showLogin():void {
                    // Create the TitleWindow container.
                var helpWindow:EditStudentLogInForm = EditStudentLogInForm(
                    PopUpManager.createPopUp(this, EditStudentLogInForm, true));

                helpWindow.username.text = "johnbdh@myserver.com";
                helpWindow["cancelButton"].addEventListener("click", removeMe);   
                helpWindow["okButton"].addEventListener("click", submitData); 
            }

            // OK button click event listener.
            private function submitData(event:Event):void {
                testText.text = helpWindow.username.text;
                                //*********helpWindow is nul*******
                removeMe(event);
            }

            // Cancel button click event listener.
            private function removeMe(event:Event):void {
                PopUpManager.removePopUp(helpWindow);
            }                           
        ]]>
    </mx:Script>    
</mx:Application>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you do

public function showLogin():void {
    var helpWindow:EditStudentLogInForm = ...
}

you're declaring and instantiating a new variable helpWindow inside the scope of the showLogin method. This means that the instance you assigned to this locally scoped variable can not be accessed outside the showLogin method.

You did declare another variable helpWindow on the class scope (your class being the main application in this case), but you're never assigning any instance to it (since you're assigning this popup instance to the helpWindow variable that lives only in showLogin.

Hence when you try to access this variable in another method, it's value is null.
The solution is simple enough: just assign the popup instance to the class-scoped variable:

public function showLogin():void {
    helpWindow = EditStudentLogInForm(
        PopUpManager.createPopUp(this, EditStudentLogInForm, true)
    );
    ...
}

On a side note: if you have a variable of the same name on the class and inside a method, the most locally scoped one always takes precedence:

public var s:String = 'class';

public function myMethod():void {
    var s:String = 'method';
    trace(s);      // prints method
    trace(this.s); // prints class
}

public function myOtherMethod():void {
    trace(s);      // prints class
    trace(this.s); // prints class
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...