2010-05-19 80 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