【问题标题】:AS3: How to make Parent Function return the selected value of a ComboBoxAS3:如何使父函数返回组合框的选定值
【发布时间】:2010-12-02 04:16:21
【问题描述】:

我正在开发一个 AIR 应用程序,该应用程序需要一个仅在第一次运行时才显示的菜单。用户可以在其中选择运行应用所需的语言。

我显示这个菜单没有问题,但我需要它保持可见直到“选择语言”组合框被更改,然后返回所选选项的数据值。

我的问题是,只有在组合框更改后,我似乎无法弄清楚如何返回值。

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
        }


}

非常感谢任何帮助。 干杯!

【问题讨论】:

    标签: flash actionscript-3 combobox event-handling return-value


    【解决方案1】:

    您不能从事件处理程序返回函数。一个函数也不能从这样的嵌套函数返回一个函数。此外,函数一被调用就会返回一个值——你不能推迟它直到事件发生。只是一条建议,嵌套命名函数通常不是一个好习惯。

    我不清楚你想要达到的最终结果,但你可能想要更接近这个的结果:

    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
    }
    

    【讨论】:

      【解决方案2】:

      这对我有用,我正在使用带有 Hero SDK 的 FB Burrito

      <?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>
      

      【讨论】:

        猜你喜欢
        • 2011-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多