动态地添加一个标签到actionscript标签栏



我试图在运行时添加一个新的选项卡到actionscript TabBar控件。当我使用addChild()将选项卡添加到选项卡栏时,我在运行时得到一个异常:

Error: addChild() is not available in this class. Instead, use addElement() or modify the skin, if you have one.

然而,当我尝试使用addElement()时,我在编译时得到一个错误:

1061: Call to a possibly undefined method addElement through a reference with static type spark.components:TabBar.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               width="100%"
               height="100%">
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        private var _lastAddedNumber:int = 1;
        protected function btnAddNewTab_clickHandler(event:MouseEvent):void
        {
            tabby.dataProvider.addItem('New '+_lastAddedNumber);
            _lastAddedNumber++;
        }
    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:Button id="btnSave"
           click="btnAddNewTab_clickHandler(event)"
           label="Add New Tab"/>
<mx:Form top="50">
    <mx:FormItem label="tabWidth:">
        <s:HSlider id="slider"
                   minimum="40"
                   maximum="120"
                   value="100"/>
    </mx:FormItem>
</mx:Form>
<s:TabBar id="tabby"
          horizontalCenter="0"
          verticalCenter="0">
    <s:layout>
        <s:HorizontalLayout gap="-1"
                            columnWidth="{slider.value}"
                            variableColumnWidth="false"/>
    </s:layout>
    <s:dataProvider>
        <s:ArrayList source="[red,orange,yellow,green,blue]"/>
    </s:dataProvider>
</s:TabBar></s:Application>

可能会对你有所帮助!!

最新更新