经常有人问怎么在flex里面两个窗体里如何传递参数,下面做个简单的参数传递
这个是所谓的"父窗口"代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.binding.utils.BindingUtils;
import mx.managers.PopUpManager;
//打开窗口 并将参数传过去
private function newForm():void{
var p:title=new title();
p.text=mytext.text;
p.callback=closeHandler;
PopUpManager.addPopUp(p,this);
}
//关闭打开窗口时处理
private function closeHandler(text:String):void{
mytext.text=text;
}
]]>
</mx:Script>
<mx:TextInput id="mytext" name="mytext" x="87" y="67"/>
<mx:Button x="295" y="67" label="Button" click="newForm()"/>
</mx:Application>
下面是所谓的 title.mxml "子窗口"
<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" initialize="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.controls.TextInput;
import mx.managers.PopUpManager;
public var text:String;
public var callback:Function;//定义关闭窗口后调用的函数
private function init():void{
mytext.text=text;
}
//关闭时传参数给父窗窗口
private function onclose():void{
if(callback!=null){
callback(mytext.text);
}
PopUpManager.removePopUp(this);
}
]]>
</mx:Script>
<mx:TextInput id="mytext" x="92" y="51"/>
<mx:Button x="187" y="171" label="Button" click="onclose()"/>
</mx:TitleWindow>