简体   繁体   English

从发生事件的类返回值:ADDED_TO_STAGE

[英]Return value from class that has event: ADDED_TO_STAGE

I have a class with ADDED_TO_STAGE event (it's an incomplete dialog), and I want it to return a value. 我有一个带有ADDED_TO_STAGE事件的类(这是一个不完整的对话框),我希望它返回一个值。 Is this possible? 这可能吗? Or what's the what's the way I should follow? 还是我应该遵循的方式是什么?

My class: http://sudrap.org/paste/text/555684/ 我的课: http//sudrap.org/paste/text/555684/

Edit: I'll create some buttons in the class. 编辑:我将在课堂上创建一些按钮。 I want buttons' functions to return some value. 我希望按钮的功能返回一些值。 And I'll need to get which button is clicked. 然后,我需要单击哪个按钮。 I want to pass these values to main fla. 我想将这些值传递给fla。

I'll probably need the class to return an array for settings specified in the dialog. 我可能需要该类为对话框中指定的设置返回一个数组。

Generally speaking what you have there is a view . 一般来说,您所拥有的是一个view Something visual that the user can interact with. 用户可以与之交互的视觉效果。

You should define your view in that class. 您应该在该类中定义view Apparently you want to pass a bunch of comboboxes and checkboxes to your class. 显然,您希望将一堆组合框和复选框传递给您的班级。 This is not the way to do this. 不是这样做的方法。

comboboxes and checkboxes are themselves views. 组合框和复选框本身就是视图。 They are also called components (because they provide additional functionality like styling, etc). 它们也称为组件(因为它们提供了样式等其他功能)。 If your popup should be made up of comboboxes and checkboxes that's fine. 如果您的弹出窗口应该由组合框和复选框组成,那很好。 Create them in your class PopupDialog, but do not pass them to the class. 在您的类PopupDialog中创建它们,但不要将它们传递给该类。

The thing that you should pass to your class is the model , that is the data that the user should be able to modify. 您应该传递给类的东西是model ,即用户应该能够修改的数据。

In the realm of Flash's components, this is a DataProvider object. 在Flash组件的范围内,这是一个DataProvider对象。 Take a look at the description of the .dataProvider property of the ComboBox class: 看一下ComboBox.dataProvider属性描述

Gets or sets the data model of the list of items to be viewed. 获取或设置要查看的项目列表的数据模型。 A data provider can be shared by multiple list-based components. 一个数据提供者可以被多个基于列表的组件共享。 Changes to the data provider are immediately available to all components that use it as a data source. 将数据提供程序更改为所有将其用作数据源的组件都可立即使用。

As you can see the model or dataProvider holds some data. 如您所见, modeldataProvider包含一些数据。 It is an object that is shared by all views that display that very same data. 它是一个显示所有相同数据的所有视图共享的对象。 As soon as this data is manipulated in any way, all other views are notified of that change. 一旦以任何方式操纵此数据,就会将该更改通知所有其他views

I hope you can see how this solves your problem: it doesn't matter if your view is a popup or not. 我希望您能看到这是如何解决您的问题的:不管您的view是否弹出,都没有关系。 As long as it operates on a model that is shared with the rest of your application, the functionality to return the value is already there. 只要它在与您的应用程序其余部分共享的model上运行,返回值的功能就已经存在。

You can do this by creating your custom event class that extends Event and pass your required parameters within. 您可以通过创建可扩展Event并在其中传递所需参数的自定义事件类来实现。

    package{
    import flash.events.Event;
    public class CustomEvent extends Event{

        public static const PASS_PARAMS:String = "passParams";

        // Your custom properties.
        public var btnName:*;

        public function CustomEvent(type:String, bName:*, bubbles:Boolean=false, cancelable:Boolean=false):void {
            this.btnName = bName;
            super(type, bubbles, cancelable);
        }

        override public function clone():Event{
            return new CustomEvent(type,btnName, bubbles, cancelable);
        }
     }
   }

and dispatch your customEvent on button click event whenever happens 并在发生按钮单击事件时分派您的customEvent

    btn.addEventListener(MouseEvent.CLICK, onBtnClick);
    private function onBtnClick(e:MouseEvent):void{
        dispatchEvent(new Event(CustomEvent.PASS_PARAMS, e.target.name));
    }

and add a listener on your Main class 并在您的Main类上添加一个侦听器

    this.addEventListener(CustomEvent.PASS_PARAMS, onProcessedEvent);       
    private function onProcessedEvent(e:CustomEvent):void{
        trace(e.btnName);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM