In my Titanium app, I have a form with 6 identical fields, the only difference being their title. Instead of repeating code (bad practice, more work, etc.) I used a for-loop to dynamically create each field with 2 labels and a view:
//Required info
var startht = 30;
var sz = 40;
var width = '90%';
var labelgrp1 = new Array('Field 1', 'Field 2', 'Field 3', 'Field 4', 'Field 5', 'Field 6');
var viewbg = '#D4D4D4';
//For the first group of labels
for(var i=0; i<labelgrp1.length; i++) {
var view = Titanium.UI.createView({
borderColor:'#000',
backgroundColor:viewbg,
touchEnabled: true,
width:width, height:40,
top: startht + i * (sz - 1)
});
var label = Ti.UI.createLabel({
color: '#303030',
font: { fontSize:16 },
shadowColor: '#aaa',
shadowOffset: {x:1, y:1},
text: labelgrp1[i],
left:10,
width: 'auto', height: 'auto'
});
var symbol = Ti.UI.createLabel({
color: '#303030',
font: { fontSize:14 },
right:10,
text: '>',
width: 'auto', height: 'auto'
});
view.add(label);
view.add(symbol);
win.add(view);
}
I tried adding this to the bottom:
view.addEventListener('focus',function(e){
Ti.API.info("Clicked on " + label.text);
});
but to no avail. Is there a way to dynamically create event listeners or am I required to have a separate view object for each field so that it can be tied directly to an event listener?