ViewStackを使って、画面を切り替えるにはindexかidを使うけど、
Class名を使うという手もあるよ、という記事から。
Flex cookbook beta
Selecting a view in a ViewStack just by its Class
実際に書くとこんな感じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | < ?xml version="1.0" encoding="utf-8"?> < mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"> < mx:Script> < ![CDATA[ import mx.core.ComponentDescriptor; // 切り替えのスイッチになる変数 [Bindable] public var actualView:Class = TitleWindow; private function getView(viewClass:Class):int{ // ViewStackの持つ子画面の配列を取得 var descriptors:Array = views.childDescriptors; for(var i:int=0;i<descriptors .length;i++){ // 各子画面のtypeプロパティでクラス名を取得 // そのクラス名からIndexを得る var descriptor:ComponentDescriptor = descriptors[i]; if(viewClass==descriptor.type) return i; } // クラス名がどれともマッチしなければエラー throw new Error("View of " + viewClass + " not found."); } ]]> < /mx:Script> < mx:ViewStack id="views" selectedIndex="{getView(actualView )}" width="100%" height="100%"> < mx:TitleWindow title="TitleWindowView" width="80%" height="80%"/> < mx:Panel title="PanelView" width="80%" height="80%"/> < /mx:ViewStack> < mx:HBox width="100%"> < mx:Button label="TitleWindow" click="actualView=TitleWindow"/> < mx:Button label="Panel" click="actualView=Panel"/> < /mx:HBox> < /mx:Application> |
この方法を使うと、ViewStackに積むそれぞれのViewを全部違うクラスにしなくちゃいけないので、結局id使ってちゃんとそれぞれ名前付けておくのが一番良いのかなぁ。という感じですね。
でもそれなりの規模のものを作っていて、それぞれの画面をきちんとコンポーネント化して作るような場合には結構便利なのかもしれないですね。
Add Your Comment