Popup窗口访问父窗口的4种方法以及相互传值
2009-12-09 15:08
375 查看
1.如果使用MVC框架,相信这并不是一个问题。而如果没有使用的话,可以用类似的方法设置一个单例,子窗口和父窗口通过这个单例来交互消息,如果需要解耦,请发送自定义事件。总之,只要按照MVC思路来做就可以了。
2.类似JS,在子窗口的构造函数里增加一个参数,将父窗口传参进去。MXML没有构造函数,用一个属性来保存父窗口引用也可以。
3.无论是createPopUp还是addPopUp,他们都有一个返回值,得到子窗口的实例。可以对这个实例监听remove事件,并在这个事件中直接读取子窗口需要返回给父窗口的属性。(记得要将这个事件最终移除)
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
" layout="absolute">
<mx:Panel x="94" y="178" width="503" height="347" layout="absolute">
<mx:TextInput x="134" y="64" id="tit_usr" text="username"/>
<mx:TextInput x="134" y="125" id="tit_psw" text="password"/>
<mx:Button x="171" y="209" label="Submit" click="mytw_click()"/>
</mx:Panel>
<mx:Script>
<![CDATA[
import mx.containers.TitleWindow;
import mx.managers.PopUpManager;
import mx.controls.Text;
private var tw:titlewindow=new titlewindow();
private function mytw_click():void{
if(tw.visible){
PopUpManager.removePopUp(tw);
}
PopUpManager.addPopUp(tw,this);
PopUpManager.centerPopUp(tw);
tw.addEventListener("tw_click",update);
}
private function update(event:Event):void{
tit_usr.text=tw.tw_usr.text;
tit_psw.text=tw.tw_psw.text;
PopUpManager.removePopUp(tw);
}
]]>
</mx:Script>
</mx:Application>
弹出窗口:
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml
" layout="absolute" width="498" height="368" showCloseButton="true" close="PopUpManager.removePopUp(this)">
<mx:Label x="96" y="67" text="username" width="97" height="26"/>
<mx:Label x="96" y="128" text="password" width="97" height="24"/>
<mx:TextInput x="217" y="65" id="tw_usr"/>
<mx:TextInput x="217" y="126" id="tw_psw"/>
<mx:Button x="228" y="239" label="Click" click="btn_click()"/>
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
import mx.controls.Text;
private function btn_click():void{
dispatchEvent(new Event("tw_click"));
}
]]>
</mx:Script>
</mx:TitleWindow>
4.
owner property
owner:DisplayObjectContainer [read-write]
The
owner of this UIComponent. By default, it is the parent of this
UIComponent. However, if this UIComponent object is a child component
that is popped up by its parent, such as the dropdown list of a
ComboBox control, the owner is the component that popped up this
UIComponent object.
This property is not managed by Flex, but
by each component. Therefore, if you use the PopUpManger.createPopUp()
or PopUpManger.addPopUp() method to pop up a child component, you
should set the owner property of the child component to the component
that popped it up.
The default value is the value of the parent property.
我也不翻译了- -说真的我看到了感觉很囧。owner这个属性在Popup中没有用途,因此,可以在创建窗口的时候将子窗口的owner设置成父窗口(也就是当时的this),然后子窗口访问自己的owner属性即可。
父窗口代码:PopUpDemo.mxml
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
" layout="absolute">
<mx:Panel x="94" y="178" width="503" height="347" layout="absolute">
<mx:TextInput x="134" y="64" id="tit_usr" text="username"/>
<mx:TextInput x="134" y="125" id="tit_psw" text="password"/>
<mx:Button x="171" y="209" label="Submit" click="mytw_click()"/>
</mx:Panel>
<mx:Script>
<![CDATA[
import mx.containers.TitleWindow;
import mx.managers.PopUpManager;
import mx.controls.Text;
private var tw:titlewindow=new titlewindow();
private function mytw_click():void{
tw.owner = this;
PopUpManager.addPopUp(tw,this);
PopUpManager.centerPopUp(tw);
}
]]>
</mx:Script>
</mx:Application>
弹出窗口代码:titlewindow.mxml
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml
" layout="absolute" width="498" height="368" showCloseButton="true" close="PopUpManager.removePopUp(this)">
<mx:Label x="96" y="67" text="username" width="97" height="26"/>
<mx:Label x="96" y="128" text="password" width="97" height="24"/>
<mx:TextInput x="217" y="65" id="tw_usr"/>
<mx:TextInput x="217" y="126" id="tw_psw"/>
<mx:Button x="228" y="239" label="Click" click="btn_click()"/>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
import mx.controls.Text;
private function btn_click():void{
//dispatchEvent(new Event("tw_click"));
var a:PopUpDemo = this.owner as PopUpDemo;
a.tit_usr.text = this.tw_usr.text;
a.tit_psw.text = this.tw_psw.text;
PopUpManager.removePopUp(this);
}
]]>
</mx:Script>
</mx:TitleWindow>
2.类似JS,在子窗口的构造函数里增加一个参数,将父窗口传参进去。MXML没有构造函数,用一个属性来保存父窗口引用也可以。
3.无论是createPopUp还是addPopUp,他们都有一个返回值,得到子窗口的实例。可以对这个实例监听remove事件,并在这个事件中直接读取子窗口需要返回给父窗口的属性。(记得要将这个事件最终移除)
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
" layout="absolute">
<mx:Panel x="94" y="178" width="503" height="347" layout="absolute">
<mx:TextInput x="134" y="64" id="tit_usr" text="username"/>
<mx:TextInput x="134" y="125" id="tit_psw" text="password"/>
<mx:Button x="171" y="209" label="Submit" click="mytw_click()"/>
</mx:Panel>
<mx:Script>
<![CDATA[
import mx.containers.TitleWindow;
import mx.managers.PopUpManager;
import mx.controls.Text;
private var tw:titlewindow=new titlewindow();
private function mytw_click():void{
if(tw.visible){
PopUpManager.removePopUp(tw);
}
PopUpManager.addPopUp(tw,this);
PopUpManager.centerPopUp(tw);
tw.addEventListener("tw_click",update);
}
private function update(event:Event):void{
tit_usr.text=tw.tw_usr.text;
tit_psw.text=tw.tw_psw.text;
PopUpManager.removePopUp(tw);
}
]]>
</mx:Script>
</mx:Application>
弹出窗口:
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml
" layout="absolute" width="498" height="368" showCloseButton="true" close="PopUpManager.removePopUp(this)">
<mx:Label x="96" y="67" text="username" width="97" height="26"/>
<mx:Label x="96" y="128" text="password" width="97" height="24"/>
<mx:TextInput x="217" y="65" id="tw_usr"/>
<mx:TextInput x="217" y="126" id="tw_psw"/>
<mx:Button x="228" y="239" label="Click" click="btn_click()"/>
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
import mx.controls.Text;
private function btn_click():void{
dispatchEvent(new Event("tw_click"));
}
]]>
</mx:Script>
</mx:TitleWindow>
4.
owner property
owner:DisplayObjectContainer [read-write]
The
owner of this UIComponent. By default, it is the parent of this
UIComponent. However, if this UIComponent object is a child component
that is popped up by its parent, such as the dropdown list of a
ComboBox control, the owner is the component that popped up this
UIComponent object.
This property is not managed by Flex, but
by each component. Therefore, if you use the PopUpManger.createPopUp()
or PopUpManger.addPopUp() method to pop up a child component, you
should set the owner property of the child component to the component
that popped it up.
The default value is the value of the parent property.
我也不翻译了- -说真的我看到了感觉很囧。owner这个属性在Popup中没有用途,因此,可以在创建窗口的时候将子窗口的owner设置成父窗口(也就是当时的this),然后子窗口访问自己的owner属性即可。
父窗口代码:PopUpDemo.mxml
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
" layout="absolute">
<mx:Panel x="94" y="178" width="503" height="347" layout="absolute">
<mx:TextInput x="134" y="64" id="tit_usr" text="username"/>
<mx:TextInput x="134" y="125" id="tit_psw" text="password"/>
<mx:Button x="171" y="209" label="Submit" click="mytw_click()"/>
</mx:Panel>
<mx:Script>
<![CDATA[
import mx.containers.TitleWindow;
import mx.managers.PopUpManager;
import mx.controls.Text;
private var tw:titlewindow=new titlewindow();
private function mytw_click():void{
tw.owner = this;
PopUpManager.addPopUp(tw,this);
PopUpManager.centerPopUp(tw);
}
]]>
</mx:Script>
</mx:Application>
弹出窗口代码:titlewindow.mxml
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml
" layout="absolute" width="498" height="368" showCloseButton="true" close="PopUpManager.removePopUp(this)">
<mx:Label x="96" y="67" text="username" width="97" height="26"/>
<mx:Label x="96" y="128" text="password" width="97" height="24"/>
<mx:TextInput x="217" y="65" id="tw_usr"/>
<mx:TextInput x="217" y="126" id="tw_psw"/>
<mx:Button x="228" y="239" label="Click" click="btn_click()"/>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
import mx.controls.Text;
private function btn_click():void{
//dispatchEvent(new Event("tw_click"));
var a:PopUpDemo = this.owner as PopUpDemo;
a.tit_usr.text = this.tw_usr.text;
a.tit_psw.text = this.tw_psw.text;
PopUpManager.removePopUp(this);
}
]]>
</mx:Script>
</mx:TitleWindow>
相关文章推荐
- Popup窗口访问父窗口的4种方法以及相互传值事件。
- Popup窗口访问父窗口的4种方法以及相互传值
- flex中Popup窗口访问父窗口的4种方法以及相互传值
- Flex4 Popup窗口访问父窗口的5种方法以及相互传值
- Popup窗口访问父窗口的5种方法以及相互传值
- Popup窗口访问父窗口的方法以及相互传值
- Popup窗口访问父窗口的5种方法以及相互传值
- 【转】Popup窗口访问父窗口的5种方法以及相互传值
- 【转】Popup窗口访问父窗口的5种方法以及相互传值
- 【转】Popup窗口访问父窗口的5种方法以及相互传值
- MFC框架类、文档类、视图类相互访问(及窗口句柄获取)的方法
- 父页面与子窗口Dialog相互传值的方法
- 不同窗口之间js方法,变量以及节点的访问
- Iframe的应用以及父窗口和子窗口的相互访问
- Iframe的应用以及父窗口和子窗口的相互访问
- js中使用jsp声明变量方法 + js访问servlet及相互传值方法
- iframe与主框架跨域相互访问方法
- MFC框架类、文档类、视图类相互访问的方法
- WindowManager和WindowManager.LayoutParams的使用以及实现悬浮窗口的方法
- 解析js中获得父窗口链接getParent方法以及各种打开窗口的方法