关于回调函数传参数问题[批量加载中会经常用到]

标签: dictionarystringhashmapfunctionpathcallback
1916人阅读 评论(2) 收藏 举报
分类:
方式一: 1.用内联函数传参 (只适用于一个回调加载,多个时候不行) 例:
比如我们要在callbackFunction函数中传递一个字符串'id is:'和一个整型15,实现方法请参见如下代码:

var str:String = 'id is:';

var num:int = 15;


Alert.show("是否删除该条目?","确认删除",Alert.YES|Alert.NO,this,function(e:CloseEvent):void{callbackFunction(e,str,num)});


private function callbackFunction(event:CloseEvent,str:String,num:int):void{
        if(Alert.YES == event.detail){
            //当点击YES时的操作
            Alert.show(str+num.toString(),"您点击了YES按钮");
        }else{
            //当点击NO时的操作
        }
 } 


 方式二:用自定义事件(有点小问题)

                

package {
   
   import flash.events.Event;
 
   public class CustomEvent extends Event {
     
      public static const CUSTOM:String = "custom";
     
      public var arg:*;
     
      public function CustomEvent(type:String, customArg:*=null,
                                  bubbles:Boolean=false,
                                  cancelable:Boolean=false) {
         
         super(type, bubbles, cancelable);
         
         this.arg = customArg;
         
      }
           
      public override function clone():Event {
         return new CustomEvent(type, arg, bubbles, cancelable);
      }
     
      public override function toString():String {
         return formatToString("CustomEvent", "type", "arg",
                               "bubbles", "cancelable", "eventPhase");
      }
   
   }
 
}


方式三:推荐(Dictionary)用Dictionary保存Loader

private var hashmap:Dictionary = new Dictionary ;   //存放Loader[e.target]对象
				for each(var path:String in arrpath){
					var loader:URLLoader = new URLLoader();
					loader.addEventListener(Event.COMPLETE,onLoadComplete);   //回调函数传参数
					hashmap[loader] = {path:path , i:'i'}  ;  //设置参数
					loader.load(new URLRequest(path));
				}

/**
		 * callback function for loader
		 * */
		private function onLoadComplete(e:Event):void{
			var loader:URLLoader = e.target as URLLoader ;
			var params:Object = hashmap[loader] ;
			trace(params) ;
			
			var content:String = e.target.data as String ;
			//var path:String = e.arg as String ;
		}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:796523次
    • 积分:11593
    • 等级:
    • 排名:第1314名
    • 原创:339篇
    • 转载:46篇
    • 译文:1篇
    • 评论:268条
    最新评论
    Ruby