0

我正面临这个问题,我正在从一组项目(字符串)动态生成滑块,我使用一个对象动态创建变量,然后将一个事件侦听器添加到我刚刚创建的变量中,但它不会工作。

itemsObject[labelValue]当检测到更改事件时,它没有更改其文本值的变量 。

var items = ['Love',
        'Angry',
        'Jealous',
        'Happiness',
        'Understanding'];
                    
                    
//all the controls on this view                 
var itemsObject = {};


var scrollView = Titanium.UI.createScrollView({
    contentWidth:'auto', 
    contentHeight:'auto', 
    top:0, 
    showVerticalScrollIndicator:true, 
    showHorizontalScrollIndicator:false,
    touchEnabled: true 
}); 

var viewInsideScrollView = Ti.UI.createView({ 
    width:'310dp', 
    height:'auto', 
    top:0,
    touchEnabled:false 
}); 

scrollView.add(viewInsideScrollView);


var fromTopItem  =30,
    fromTopSlider=50,
    fromTopLabelValue =25;

for(i in items){
    
    var label = items[i] + 'Label';
    //item name
    itemsObject[label] = new Ti.UI.createLabel({
        text: items[i],
        color: '#ffffff',
        top:fromTopItem,
        height:'auto',
        width:'auto'
    });

    fromTopItem+=70;
    
    var slider = items[i] + 'Slider';

    //slider control
    itemsObject[slider] = Titanium.UI.createSlider({
        top:fromTopSlider,
        min: 0,
        max: 5,
        minRange: 0,
        maxRange:5,
        width: '95%',
        value: parseInt(Math.random() * (5 - 0) + 0) //values from 0 to 5
    });
    
    fromTopSlider+=70;
    
    var labelValue = items[i] + 'LabelValue';
    
   //value selected
   itemsObject[labelValue] =  new Ti.UI.createLabel({
        text: itemsObject[slider].value,
        color: '#ffffff',
        top:fromTopLabelValue,
        left:285,
        height:'auto',
        width:'auto',
        font:{
            fontSize:24,
            fontWeight: 'bold'
        }
    });
    
    fromTopLabelValue+= 70;
    
    //listener
    itemsObject[slider].addEventListener('change', function(e){
        itemsObject[labelValue].text = parseInt(e.value);
    });
    
//adding to the view
    viewInsideScrollView.add(itemsObject[label]); 
    viewInsideScrollView.add(itemsObject[labelValue] );
    viewInsideScrollView.add(itemsObject[slider] );
    
}



self.add(scrollView);
4

1 回答 1

0

我解决了它,这是一个范围问题,所以我将控件创建放在一个函数中,然后用循环调用该函数。

于 2012-11-02T03:50:20.617 回答