2010-05-19 147 views
2

我有一个Flex 3中的视图,其中我使用标签导航器和标签导航器中的一些视图。我需要知道哪个视图被点击是因为它是一个特定的视图,然后我需要采取行动,即如果点击带有id“secondTab”的视图,则执行一些操作。单击标签时访问标签导航器中的视图

我已经设置好通知,我的问题是我需要能够知道它是什么样的视图。调用tab.GetChildByName或类似的方法似乎只让我回到TabSkin对象。

<?xml version="1.0" encoding="utf-8"?> 
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" 
width="100%" 
height="100%" 
xmlns:local="*" 
creationComplete="onCreationComplete(event)"> 

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

     protected function onCreationComplete(event:Event):void { 
      for(var i:int = 0; i < myTN.getChildren().length; i++) { 
       var tab:Button = myTN.getTabAt(i); 
       tab.addEventListener(FlexEvent.BUTTON_DOWN, tabClickHandler); 
      }    
     } 

     private function tabClickHandler(event:FlexEvent):void { 
      var tab:Button; 

      if(event.currentTarget is Button) { 
       tab = event.currentTarget as Button; 

       // how do I access the actual view hosted in a tab that was clicked? 
      } 
     } 



    ]]> 
</mx:Script> 

<mx:TabNavigator id="myTN"> 
    <local:ProductListView id="firstTab" 
          label="First Tab" 
          width="100%" height="100%" /> 
    <local:ProductListView id="secondTab" 
          label="Second Tab" 
          width="100%" height="100%" /> 
</mx:TabNavigator> 


</mx:VBox> 

回答

3

TabNavigatorViewStack一个子类,当你选择一个标签就会触发一个事件change

<mx:TabNavigator id="myTN" change="childChanged()"> 
    <local:ProductListView id="firstTab" 
          label="First Tab" 
          width="100%" height="100%" /> 
    <local:ProductListView id="secondTab" 
          label="Second Tab" 
          width="100%" height="100%" /> 
</mx:TabNavigator> 

它是那样简单:

private function childChanged():void 
{ 
    if(myTN.selectedChild == this.firstTab) //or myTN.selectedIndex == 0 
    { 
    trace("selected the first one"); 
    } 
    else if(myTN.selectedChild == this.secondTab) //or myTN.selectedIndex == 0 
    { 
    trace("selected the second one"); 
    } 
} 
+2

如果您将事件对象包含在您的childChanged(event)处理函数中,它将包含一个relatedObject属性,该属性应该是选定的子项,如果您不想引用回选项卡导航器即: change =“childChanged(event)”和私有函数childChanged(event:IndexChangedEvent):void {if(event.relatedObject == ... – quoo 2010-05-19 14:20:49

+0

谢谢,非常有帮助! – user278730 2010-05-19 15:11:59

0

由于TabNavigatorViewStack的扩展,你可以用selectedChild属性访问选定的视图:

private function tabClickHandler(event:FlexEvent):void { 
    view = myTN.selectedChild; 

    // Do what you need to do with it here... 
} 

有关如何更多信息TabNavigator的工作原理,查看文档:

http://livedocs.adobe.com/flex/3/html/help.html?content=navigators_4.html