简体   繁体   中英

AS3: How to make Parent Function return the selected value of a ComboBox

I'm developing an AIR app that requires a menu to show only during the first run. In it the user will be able to choose the desired language for the app to run in.

I'm displaying this menu without a problem but I need it to stay visible until the "select language" comboBox is changed and then return the selected choice's data value.

My problem is that I can't seem to figure out how to return a value only after the combo box is changed.

function promptFRMenu():String{
FRMenu.enabled = FRMenu.visible = true; //when I detect the app is running for the             
                                       // first time, the dialog box is enabled 
                                       // and made visible
var peferedLng:String = new String;

    FRMenu.language_CBox.addEventListener(Event.CHANGE, announceSelectedItem);
        function announceSelectedItem(e:Event):void {
                 FRMenu.enabled = FRMenu.visible=false;
                 peferedLng = e.target.selectedItem.data;
                -> return peferedLng;  
                //It is the 'parent' function that should return this value but 
               // only after it is selected
        }


}

I'd really appreciate any help. Cheers!

This works for me, I am using FB Burrito w/the Hero SDK

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            private function _handleChange($event:Event) :void
            {
                trace($event.target + " // CHANGED // " + $event.target.selectedItem.value);
            }
        ]]>
    </fx:Script>

    <mx:ComboBox id="comboBox" rowCount="5" labelField="label" prompt="Select One" change="_handleChange(event);">
        <mx:dataProvider>
            <s:ArrayList>
                <fx:Object label="One" value="1" />
                <fx:Object label="Two" value="2" />
                <fx:Object label="Three" value="3" />
                <fx:Object label="Four" value="4" />
                <fx:Object label="Five" value="5" />
            </s:ArrayList>
        </mx:dataProvider>
    </mx:ComboBox>

</s:Application>

you can't return a function from an event handler. also a function can't return a function from a nested function like that. further, a function will return a value as soon as it's invoked - you can't defer it until an event occurs. and just a piece of advice, it's generally not good practice to nest named functions like that.

i'm not clear on the net result you want to achieve, but you probably want something closer to this:

var peferedLng:String = new String;
function promptFRMenu():String{
  FRMenu.enabled = FRMenu.visible = true;
}
FRMenu.language_CBox.addEventListener(Event.CHANGE, announceSelectedItem);
function announceSelectedItem(e:Event):void {
   FRMenu.enabled = FRMenu.visible=false;
   peferedLng = e.target.selectedItem.data;
  // do whatever you want to do with peferedLng here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM