1

我需要使用 pop() 以便取出数组中的最后一个元素(位于组合框中),但这不起作用......请帮忙!

ps当我运行代码时,我没有收到任何错误,其他一切正常,按钮也是如此:

我的代码:

var barColours:Array = new Array();
barColours  = ["red", "green", "blue", "orange", "purple"];

comboBox.dataProvider = new DataProvider(barColours);
comboBox.addEventListener(Event.CHANGE, onChange);
comboBox.x = 670;
comboBox.y = 55;

minus_btn.addEventListener(MouseEvent.CLICK, takeAwayCol);

function takeAwayCol(ev:MouseEvent):void
{
      barColours.pop();
} 
4

1 回答 1

0

修改数组不会“通知”您需要重新分配 dataProvider 的更改列表。

var barColours:Array = new Array();
barColours  = ["red", "green", "blue", "orange", "purple"];

comboBox.dataProvider = new DataProvider(barColours);
comboBox.addEventListener(Event.CHANGE, onChange);
comboBox.x = 670;
comboBox.y = 55;

minus_btn.addEventListener(MouseEvent.CLICK, takeAwayCol);

function takeAwayCol(ev:MouseEvent):void
{
      barColours.pop();
      comboBox.dataProvider = new DataProvider(barColours);
} 

根据评论编辑

var barColours:Array = ["red", "green", "blue", "orange", "purple"];
var removedBarColours:Array = new Array();

comboBox.dataProvider = new DataProvider(barColours);
comboBox.addEventListener(Event.CHANGE, onChange);
comboBox.x = 670;
comboBox.y = 55;

minus_btn.addEventListener(MouseEvent.CLICK, takeAwayCol);

function takeAwayCol(ev:MouseEvent):void
{
//if you need to pull from the beginning of the list instead of the end use shift/unshift method(s) of Array
      removedBarColours.push(barColours.pop());
      comboBox.dataProvider = new DataProvider(barColours);
} 

function addCol(ev:MouseEvent):void
{
      barColours.push(removedBarColours.pop());
      comboBox.dataProvider = new DataProvider(barColours);
} 
于 2012-11-07T20:38:05.187 回答