我有一些按钮切换设置为true。我正在尝试将按钮的状态重新设置为未选中状态。但是,我无法直接访问按钮,如:button.selected = false。
我正在访问HBox的儿童按钮。 UIComonent没有选定的属性。那么,如何在下面的代码中取消选择切换?
for (var j : int=0; j < theHBox.numChildren; j++){
var child : DisplayObject = theHBox.getChildAt(j);
var myButton:UIComponent = child as UIComponent;
myButton.setStyle("borderColor", "blue");
myButton.visible = true;
}
答案 0 :(得分:2)
如果可能,我建议将UIComponent转换为按钮:
for (var j : int=0; j < theHBox.numChildren; j++){
var child : DisplayObject = theHBox.getChildAt(j);
if(child is Button){
var myButton:Button = child as Button;
myButton.setStyle("borderColor", "blue");
myButton.visible = true;
} else if(child is somethingElse){
// do something else
}
}
您也可以这样做:
for (var j : int=0; j < theHBox.numChildren; j++){
var child : DisplayObject = theHBox.getChildAt(j);
var myButton:UIComponent = child as UIComponent;
myButton.setStyle("borderColor", "blue");
myButton.visible = true;
myButton['toggle'] = false;
}
如果所有子节点都是按钮,那将起作用,但如果'myButton'没有切换属性,则会抛出运行时错误。