flex 3:使用警报窗口将值传递给函数

时间:2011-05-12 18:59:58

标签: flex alert

我有一个功能,可以在用户单击按钮时检查某些内容。如果找到了这个东西,就会出现一个警告,说它已被发现并询问他们是否愿意允许这种情况发生,或者撤消导致找到某些东西的动作。代码如下所示:

Alert.show(thisString1, "Conflict: Multiple Projects", 3, this, conflictAnswer);

按下“是”或“否”,将调用conflictAnswer函数......它看起来像这样:

private function conflictAnswer(event:CloseEvent):void
{
    if (event.detail == Alert.YES)
    {
        Alert.show(
    }
}

我的问题是,如何传递显示警报的函数中保存的一些变量?我试过这样的事情:

Alert.show(thisString1, "Conflict: Multiple Projects", 3, this, conflictAnswer(Event, var1, var2));


private function conflictAnswer(event:CloseEvent, varA, varB):void
{
    if (event.detail == Alert.YES)
    {

    }
}

但它不起作用。

有人可以帮帮我吗?

由于 BRDS

修改 的 看完第一个回复后,我想出了这个:

answers[0] = cPositions[i][0];
answers[1] = cPositions[i][1];
var anAlert:Alert = Alert.show(thisString1, "Conflict: Multiple Projects", 3, this, conflictAnswer);
anAlert.data = {answers:Array};

然后conflictAnswer函数如下所示:

private function conflictAnswer(event:CloseEvent):void
{
    var projectID:Number = event.currentTarget.answers[0];
    var positionID:Number = event.currentTarget.answers[1];
    if (event.detail == Alert.YES)
    {
        Alert.show(String(projectID + " | " + positionID));
    }
}

但这不起作用......任何想法?

1 个答案:

答案 0 :(得分:3)

Alert.show()会返回一个警告实例,其中包含data字段,您可以在其中设置数据:

var anAlert:Alert = Alert.show(thisString1, "Conflict: Multiple Projects", 3, this, conflictAnswer);
anAlert.data = {var1:var1, var2:var2};

然后在事件处理程序中,您可以获取数据对象:

var myData:Object = event.currentTarget.data;
var var1:Object = myData.var1;
var var2:Object = myData.var2;

对于您的代码,它将如下所示:

answers[0] = cPositions[i][0];
answers[1] = cPositions[i][1];
var anAlert:Alert = Alert.show(thisString1, "Conflict: Multiple Projects", 3, this, conflictAnswer);
anAlert.data = {answers:answers};

然后:

private function conflictAnswer(event:CloseEvent):void
{
    var projectID:Number = event.currentTarget.data.answers[0];
    var positionID:Number = event.currentTarget.data.answers[1];
    if (event.detail == Alert.YES)
    {
        Alert.show(String(projectID + " | " + positionID));
    }
}