0

我有一个正在处理的 Flex 移动项目。我正在尝试将参数从一个视图传递到另一个视图。唯一的问题是我不能使用 navigator.pushView 来推送视图和参数,因为我推送到的视图是前一个视图。所以这使用 addHandler() 和 returnObjectsCreated() 消除了,因为我不能使用 pushView。我不得不使用 popView,因为我之前的观点是我也必须传递参数。任何帮助,将不胜感激。

具有我需要传递的参数的类如下。这是一个显示列表的视图。所以我需要将 list.selectedItem 传递给弹出视图或上一个视图...

<?xml version="1.0" encoding="utf-8"?>
<amec:BaseBrowseView xmlns:fx="http://ns.adobe.com/mxml/2009" 
              xmlns:s="library://ns.adobe.com/flex/spark" 
              xmlns:amec="com.amec.Components.*"
              title="Select an item">

<fx:Script>
    <![CDATA[
        import com.amec.BaseSql;

        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;

        import spark.events.IndexChangeEvent;

        [Bindable]private var resultArr:ArrayCollection = new ArrayCollection();

        protected function myList_changeHandler(event:IndexChangeEvent):void 
        {

            navigator.popView();


            //Either send a ref to the last view or override createReturn

        }


        [Bindable(event="myDataChanged")]
        private function get myData():ArrayCollection
        {

                           }



    ]]>
</fx:Script>

<s:List id="list"         
        height="100%" width="100%" 
        dataProvider="{myData}" 
        labelField="DMV_VALUE_1"
        change="myList_changeHandler(event);">
</s:List>


<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

现在下面是我想要 popView 的上一个视图,我需要将参数传递给它,以便我可以填充 TextInput。

<?xml version="1.0" encoding="utf-8"?>
<amec:BaseControl xmlns:fx="http://ns.adobe.com/mxml/2009" 
              xmlns:s="library://ns.adobe.com/flex/spark" 
              xmlns:amec="com.amec.Components.*"
              horizontalCenter="true">

<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        [Bindable]
        protected var textValue:String;

        protected function control_creationCompleteHandler(event:FlexEvent):void
        {
            // todo: get control data from view.data


        }

        protected function control_clickHandler(event:MouseEvent):void
        {

            parentView.navigator.pushView(TextListView);

        }




    ]]>
</fx:Script>

<s:Label text="Robert says something meaningful goes here" />

<s:TextInput id="ns" text="{textValue}" editable="false" click="control_clickHandler(event)"/>





<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

同样,我不能使用 pushView,因为视图已经在堆栈上。

4

2 回答 2

2

如果您想以正确的方式进行操作,请在上一个视图中(您希望结果转到的视图,为“添加”添加一个事件监听器。

protected function addHandler(event:FlexEvent) : void {
if (navigator.poppedViewReturnedObject != null) {
                var returnedObject : Array = navigator.poppedViewReturnedObject.object as Array;
                if(!returnedObject) return;
                yourData= new ArrayCollection(returnedObject);
            }

}

鉴于您要从以下位置传递对象:

override public function createReturnObject():Object {
            var returnedObject : Object = new Object();
            returnedObject = dataYouWantToSendBack;
            return returnedObject;
        }
于 2013-09-06T23:49:19.170 回答
-1

我研究了使用静态变量,但这有点混乱,我还研究了使用依赖注入,但是当我所做的只是在 popView 上传递参数时,花费几个小时来设置框架并让它运行是很昂贵的,所以我想出的是我在使用 Android SDK 做原生 Android 应用程序时使用的类似方法。带有 getter 和 setter 的 Singleton。基本上,我能够做到这一点的方法是使用 Singleton sharedInstance。请参见下面的代码:

package com.controls.Text
{
    import flash.utils.Dictionary;
    [Bindable]

    public class ParameterManager
    {
        private static var instance:ParameterManager = new ParameterManager();
        private var dictionary:Dictionary=new Dictionary(); 


        public function ParameterManager( )
        {
        if (instance != null) { throw new Error('Must use ParameterManager.getInstance().') }
        }

        public static function getInstance(): ParameterManager {
            return instance;
        }

        public function setParameter(key:*, value:*):void
        {
        dictionary[key]=value;  
        }

        public function getParameter(key:*):*
        {
            return dictionary[key];
            }
    }
}

设置参数值的方法:

        protected function myList_changeHandler(event:IndexChangeEvent):void 
        {
            var listViewReturnObject:String = new String();
            listViewReturnObject = list.selectedItem.DMV_VALUE_1;
            ParameterManager.getInstance().setParameter("selectedItem", listViewReturnObject);

            navigator.popView();
        }

取值方法:

        protected function control_creationCompleteHandler(event:FlexEvent):void
        {               
            var listViewReturnObject:Object = new Object();

            listViewReturnObject= ParameterManager.getInstance().getParameter("selectedItem");
            if (listViewReturnObject != null)
            {
                dataTxt.text= String(listViewReturnObject);
                ParameterManager.getInstance().setParameter("", null); //Make sure objects are cleared when done.
            }
        }
于 2013-09-03T20:53:52.137 回答