如何捕获警报对话框选择?

时间:2010-10-05 18:31:49

标签: flex actionscript-3

在事件处理程序中,我有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)
 ...
 ...

1 个答案:

答案 0 :(得分: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);
            ...
         }
      }
  );
}