2
votes

I I have a view in Flex 3 where I use a tab navigator and a number of views inside the tab navigator. I need to be know which view was clicked because of it's one specific view then I need to take action, i.e. if view with id "secondTab" is clicked then do something.

I have set it up to be notified, my problem is that I need to be able to know what view it is. Calling tab.GetChildByName or a similar method seems to only get me back a TabSkin object.

<?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>
2

2 Answers

3
votes

TabNavigator is a subclass of ViewStack and it will fire a change event when you select a tab.

<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>

It is as straightforward as:

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");
  }
}
0
votes

As TabNavigator is an extension of ViewStack you can access the selected view with the selectedChild property:

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

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

For more information on how TabNavigator works, check out the Documentation:

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