【问题标题】:How to capture Alert Dialog Box Selection?如何捕获警报对话框选择?
【发布时间】:2010-10-05 18:31:48
【问题描述】:

在事件处理程序中,我有一个Alert.show(...),它会提示用户进行确认。如何捕获警报提示的选择并在事件处理程序中使用它。例如:

private function mainEvtHandler(event:DynamicEvent):void {
 var alert:Alert = Alert.show("Are you sure?", "Confirmation", Alert.YES|Alert.NO, this, alertHandler);
 // How can I retrieve the selection and use it within this event handler?
 // i.e. if (alert == Alert.Yes) { ...
 var index:int = arrayColl.getItemIndex(event.data)
 ...
 ...

【问题讨论】:

    标签: apache-flex actionscript-3


    【解决方案1】:

    您可以将alertHandler 声明为嵌套函数 ...

    private function mainEvtHandler(event:DynamicEvent):void {
    
      var alertResult: int = -1;
    
      function alertHandler(evt:CloseEvent):void {
         alertResult = evt.detail;
      }
    
      var alert:Alert = Alert.show("Are you sure?", "Confirmation", Alert.YES|Alert.NO, this, alertHandler);
      if (alertResult == Alert.Yes) {
         var index:int = arrayColl.getItemIndex(event.data);
      ...
    
    }
    

    ...或者您可以使用匿名函数

    private function mainEvtHandler(event:DynamicEvent):void {
      Alert.show("Are you sure?", "Confirmation", Alert.YES|Alert.NO, this, 
          function (nestedCloseEvent:CloseEvent):void {
             if (nestedCloseEvent.detail == Alert.Yes) {
                var index:int = arrayColl.getItemIndex(event.data);
                ...
             }
          }
      );
    }
    

    【讨论】:

    • 不要忘记 alertHandler 也可以是它自己的独立函数,在 mainEvtHandler 之外定义。
    • @Wade,我认为问题的根源在于对event.data 的直接访问,可以将其设为全局并在外部处理,但我想这就是 OP 想要节省的。
    • 是的,我的偏好是将“事件”变量保留在处理程序的本地,而不是使其成为全局变量。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-08-29
    • 2018-08-27
    • 2013-06-26
    • 2022-07-18
    • 1970-01-01
    • 2013-04-10
    • 2017-09-14
    相关资源
    最近更新 更多