Я работаю в JavaScript с Titanium для разработки мобильных приложений. У меня проблема в цикле for с счетчиком bn int. Кнопка [bn] .setTitle ('*') всегда будет 5 на всех кнопках, созданных в обработчике событий, или, если я раскомментирую // bn = 0 ;, который находится вне цикла for, обновятся все кнопки, чтобы значение ноль.
На мой взгляд, это должно быть присвоение значения каждой кнопке, обработчику события и т. Д. Во время создания, а не возвращаться и изменять его по мере продвижения счета. Что мне здесь не хватает или нужно делать по другому?
/**
* create a view object to hold the buttons
* and add the buttons into the view
*/
function createRatingButtons(numButtons,BarTitle,topspace) {
// set vars
var bn=0;
var left = 5;
var top = 5;
/*
* create a view for the buttons
*/
var ratingView = Titanium.UI.createView({
height: 100,
color: 'white',
top: topspace,
});
/*
* create a label to put into the view
*/
var ratingLabel = Titanium.UI.createLabel({
text: BarTitle,
color: '#fff',
backgroundColor: 'transparent',
textAlign: 'left',
height: 'auto',
width: 'auto',
top: 0,
left: left,
})
ratingView.add(ratingLabel);
/*
* do the for loop and add
* the buttons to the view
*/
var button = [];
for(bn==0;bn<numButtons;bn++) {
button[bn] = Titanium.UI.createButton({
title: bn,
width: 50,
height: 50,
color: "black",
// backgroundColor: "blue",
left: left,
top: top+ratingLabel.getHeight(),
});
/*
* Add event handler for this button
*/
button[bn].addEventListener('click', function(e)
{
Ti.API.info("Rating Button Click #: " + bn);
/*
* Update buttons below this count for this object
* to have colored stars, and all starts after this
* to be uncolored.
*/
button[bn].setTitle('*')
});
ratingView.add(button[bn]);
left = left + 50 + 5;
}
//bn = 0;
// return the entire block for this view
return ratingView;
}