I am trying to add a button to a group in a Flex 4 application from within a function like this;
public function addButton(myID:Number):void {
var myButton:Button = new Button();
myButton.id = ObjectUtil.toString(myID);
myButton.label = "New Button "+myButton.id;
myButton.click= textAlerter(myID); 开发者_运维知识库
myGroup3.addElement(myButton);
}
The label and id properties get added but not the click. The error message says click is an undefined property for spark.components:Button. But it is a property when add it to the group like this;
What am I doing wrong?
In MXML, click
is not a property, it's a shortcut to an event listener.
public function addButton(myID:Number):void {
var myButton:Button = new Button();
myButton.id = myID.toString();
myButton.label = "New Button "+myButton.id;
myButton.addEventListener(MouseEvent.CLICK, textAlerter);
myGroup3.addElement(myButton);
}
private function textAlerter(e:Event):void
{
var myID:String = (e.currentTarget as Button).id;
//your code here
}
精彩评论