使用钛金属按钮循环一个数组

时间:2013-10-07 14:34:08

标签: titanium

我是学生,所以我为提出这么简单的问题而道歉。我试图制作一个“下一个”和“前一个”按钮,一次循环一个数组。

var food = ["pasta", "Salad", "Apple", "Pizza"];
    counter=0;

    var displayAlert = function(){ 
        for (var i=0, item; i<food.length; i++) {
        item = food[i];
        quoteText.text=food[i];
}   
};      

    var quoteView = Ti.UI.createView({
    backgroundColor: "#fff",
    height: 150,
    top: 50,
    left: 20,
    right: 20,
    borderRadius: 5

});


var quoteText = Ti.UI.createLabel({
    text: "click below to begin",
    font: {fontSize: 20, fontFamily: "Arial"},
    textAlign: "center"
});
    quoteView.add(quoteText);
    mainWindow.add(quoteView);



buttonPrevious.addEventListener("click", displayAlert);
buttonNext.addEventListener("click", displayAlert);

1 个答案:

答案 0 :(得分:0)

所以我已经和我的导师一起完成了代码工作,虽然我会对这项任务采取零,但其他人可能会从我的不幸中受益。

var food = ["pasta", "Salad", "Apple", "Pizza"];
    counter=0;

    var displayAlert = function(){
        console.log (counter);
    quoteText.text=food[counter];
    if (counter === food.length-1){
        counter=0;  
    }
    else {counter=counter+1};
};      
    var getPrevious = function(){
        quoteText.text=food[counter];
    if (counter === 0){
        counter=food.length-1;
    }
    else {counter=counter-1};

};
    var quoteView = Ti.UI.createView({
    backgroundColor: "#fff",
    height: 150,
    top: 50,
    left: 20,
    right: 20,
    borderRadius: 5

});


var quoteText = Ti.UI.createLabel({
    text: "click below to begin",
    font: {fontSize: 20, fontFamily: "Arial"},
    textAlign: "center"
});
    quoteView.add(quoteText);
    mainWindow.add(quoteView);