开发者

Add element to a view of a mobile application in actionScript

I have a program with a list of item. An item is an actionScript class that extends View. When I click on the list, it push the view. In the constructor of this class I add some buttons and I have a function that add another button. My problem is that the view display only the button create in the constructor and not the one create in the function.

The class

package 
{   

    import spark.components.Button;

    public class Application extends View
    {       
        public function Application()
        {
            var bt:Button = new Button();
            bt.label = "In C";
            addElement(bt);
        }

        pu开发者_JAVA百科blic function addButton():void {          
            var b:Button = new Button();
            b.label = "Olé";
            addElement(b);
            visible = true;         
        }       
    }
}

The firstView

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="Home"
    visible="false" creationComplete="retrieveApplication(event)">
<fx:Script>
    <![CDATA[
        import ...

        protected function retrieveApplication(event:FlexEvent):void
        {
            ...
            var application:Application = new Application();
            ...                     
            application.addButton();
            this.visible = true;
        }

        protected function launchApplication(event:IndexChangeEvent):void
        {
            navigator.pushView(Application);
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button x="10" y="10" width="460" label="Button" click="launchApplication(event)"/>

That did I do wrong?


Notice that you pass a class reference to the pushView , not an instance. That means you don't pass the application instance you created in the retrieveApplication method.

Flex instanciates itself an instance of the view after you call pushView. So the addButton method is never called.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜