加速器。行中的按钮不可点击
Titanium SDK 版本:1.6.2(也尝试过 1.7) iPhone SDK 版本:4.2
我正在开发一个 iPhone 应用程序,我正在从 API 中获取数据并将其呈现在表格中。在此表中,每行都有一个按钮,允许用户将该人添加到他或她的联系人中。代码的唯一问题(我认为)是只有最后一个按钮在被单击时才会响应。当我单击其他按钮时没有任何反应。
这是我的代码: http://pastie.org/1932098
有什么问题吗?
Titanium SDK version: 1.6.2 (tried with 1.7 too)
iPhone SDK version: 4.2
I am developing an iPhone app and I am fetching data from my API and presenting it in a table. In this table I got a button on each row that should allow the user to add that person to his or her contacts. The only problem with the code (I think) is that only the last button responds when being clicked. Nothing happens when I click the other buttons.
This is my code: http://pastie.org/1932098
What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要在 for 语句之外添加 button.addEventListener,并且由于每次迭代都会覆盖按钮 var,因此 eventListener 仅附加到最后创建的按钮。
这可能不是解决此问题的最佳方法,但要解决您的问题,请将 button.addEventListener 移至 for 语句内,然后检查发送到事件的对象中的唯一标识符。示例:
button.id 属性刚刚创建,但现在您可以看到哪个按钮发送事件。您还可以使用标题或任何其他独特的内容。
其他需要考虑的选项是为每个按钮创建唯一的变量名称,但这可能需要更多工作。此外,不要在表行中放置按钮,而是使用标签或图像,然后侦听表或行生成的事件。
You are adding the button.addEventListener outside of the for statement, and since you are overwriting the button var with each iteration, the eventListener only attaches to the last button created.
This probably isn't the best way to work this, but to fix your problem, move the button.addEventListener inside the for statement, and then check for a unique identifier in the object that gets sent to the event. Example:
The button.id property is just made up, but now you can see which button sends the event. You could also use title, or anything else that is unique.
Other options to look at are creating unique variable names for each button, but that's probably more work. Also, instead of working with putting a button in the table row, use a label or image, then listen for the event generated by the table or row.