如何响应在View Stack中选择的NavigatorContent

时间:2012-04-11 05:06:45

标签: actionscript-3 flex flex-spark

我在这样的应用程序中有视图堆栈:

<mx:ViewStack id="viewStack" left="0" right="0" top="0" bottom="0">
    <views:LoginView top="0" bottom="0" right="0" left="0" id="loginView"/>
    <views:MainHomeView top="0" bottom="0" right="0" left="0" id="childSelectionView"/>
    <views:MainHomeView top="0" bottom="0" right="0" left="0" id="mainEvalView"/>
</mx:ViewStack>

当一个事件被触发时,我打电话给viewStack.selectedChild = childSelectionView

我的问题是如何为正在选择的NavigatorConent编写侦听器?

这是我尝试做的事情:

<?xml version="1.0" encoding="utf-8"?>
<s:NavigatorContent xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:local="*" xmlns:mx="library://ns.adobe.com/flex/mx" 
    creationComplete="init()">

    <fx:Script>
        <![CDATA[
        private function init():void
        {
            // Do something here
            advanceButton.visablitiy = false;
        }
    ]]>
</fx:Script>
<s:VGroup>

    <s:HGroup>
        <s:Button label="Advance" id="advanceButton" />
    </s:HGroup>
</s:VGroup>

我知道所有NavigatorConent都会在应用程序启动时调度creationComplete事件,但是他们的子项尚未创建。我是如何回应关于选择儿童改变自我的?

2 个答案:

答案 0 :(得分:1)

有时候你需要花点时间来解决这个问题,从字面上看,你的问题恰好在同一个问题领域。使用FlexEvent.CONTENT_CREATION_COMPLETE事件。

请参阅Flex Error #1009: Cannot access a property or method of a null object reference

答案 1 :(得分:0)

<fx:Script><![CDATA[
    import mx.controls.Alert;
    import mx.events.IndexChangedEvent;

    protected function viewStack_childIndexChangeHandler(event:IndexChangedEvent):void {
        Alert.show('Selected Child: ' + viewStack.selectedChild.label);
    }

    ]]></fx:Script>

<s:layout>
    <s:VerticalLayout/>
</s:layout>

<s:ButtonBar dataProvider="{viewStack}"/>
<mx:ViewStack id="viewStack" change="viewStack_childIndexChangeHandler(event)">
    <s:NavigatorContent id="navigator1" label="Navigator 1">
        <s:Group>
            <s:Button label="N1"/>
        </s:Group>
    </s:NavigatorContent>
    <s:NavigatorContent id="navigator2" label="Navigator 2">
        <s:Group>
            <s:Button label="N2"/>
        </s:Group>
    </s:NavigatorContent>
</mx:ViewStack>