JavaScript Titanium App int 赋值
我正在使用 JavaScript 和 Titanium 进行移动应用程序开发。我在 for 循环中遇到 bn int 计数问题。在事件处理程序中创建的所有按钮上,button[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;
}
I'm working in JavaScript with Titanium for mobile app development. I'm having a problem down in the for loop with the bn int count. button[bn].setTitle('*') will always be 5 on all buttons created in the event handler, or if I uncomment //bn = 0;, which is outside of the for loop, it will update all buttons to be value zero.
In my mind, it should be assigning the value to each button,event handler, etc at the time of creation and not going back and changing it as it moves the count forward. What am I missing here or need to do different?
/**
* 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
您正在尝试访问匿名函数内的 bn。价值
当匿名函数执行时,将计算 bn 的值。在
此时 bn 的值将比 的值减 1
numButtons 变量。
因此请避免在匿名函数中使用 bn。你可以使用
e.source 来获取对按钮的引用。
在你的 for 循环中。
you are trying to access bn inside the anonymous function. the value
of bn will be evaluated when the anonymous function gets executes. at
that time the value of bn will be the 1 less then the value of
numButtons variable.
So avoid using bn inside the anonymous function. you can use
e.source to get the reference to the button.
in you for loop.