フレックス3:コンポーネントの孫にアクセスする方法
-
10-10-2019 - |
質問
Topboxの孫にアクセスし、それらがボタンであるかどうかを判断する必要があります。
以下のこの機能(Flextras.comに感謝します)は、HboxesであるTopboxの直接の子供をつかみます。 Topboxの孫から1つのレベルを下げるにはどうすればよいですか?それとも、これを行う唯一の方法は、各Hboxでこの関数を実行することです。
for (var i : int =0; i<topBox.numChildren; i++){
var child : DisplayObject = topBox.getChildAt(i);
var myButton:UIComponent = child as UIComponent;
blah blah
}
<mx:VBox id="topBox">
<mx:HBox id="Hbox1">
<mx:Button id="button1"
label="button1"
click="myClickHandler"/>
<mx:Button id="button2"
label="button2"
click="myClickHandler"/>
</mx:HBox>
<mx:HBox id="Hbox2">
<mx:Button id="button3"
label="button3"
click="myClickHandler"/>
<mx:Button id="button4"
label="button4"
click="myClickHandler"/>
</mx:HBox>
<mx:VBox>
解決
//declare child access function
private function processChildren(target:DisplayObjectContainer, handler:Function):void
{
for (var i:int = target.numChildren - 1; i >= 0; i--)
{
handler(target.getChildAt(i));
}
}
//call it with handler as anonymous function (may be plain one as well)
processChildren(topBox, function(target:DisplayObjectContainer):void
{
processChildren(target, function(target:DisplayObjectContainer):void
{
//this will be called on grandchildren
if (target is Button)
{
//do something
}
});
});
威圧的に見えるようにするには、匿名関数を単純な機能に置き換えることができますが、アイデアは明確でなければなりません。
所属していません StackOverflow