在Titanium中选择Id by Id

时间:2014-06-25 13:16:03

标签: javascript titanium appcelerator-mobile

在Titanium中,如何通过id选择对象?

使用Titanium我创建了一个包含子项的视图。我在父视图上设置了一个事件监听器,所以我没有必要为每个子节点创建一个事件监听器。在事件监听器中,我使用e.source.id确定单击了哪个子视图。我需要在点击时更改视图的高度,并且还需要更改打开的上一个视图的高度。 (这是为了显示带有下划线类型的活动视图。)

    var selectionView = Ti.UI.createView({
    title: 'Selection View',
    width: Ti.UI.Fill,
    left: 0,
    top: 0,
    backgroundColor: '#f5f5f5',
    layout: 'horizontal',
    height: 32,
    zIndex: 12
    });

    var selection_Type = Ti.UI.createView({
    title: "Type View",
    width: Ti.UI.Fill,
    layout: 'horizontal',
    backgroundColor: '#ABABAB',
    top: 0,
    height: 30
    });

我无法弄清楚如何通过ID名称选择对象,以便我可以更改它的高度。

//storing the id of the last clicked feed label so we can change it's height when it's no longer open
var feedSelector = 'selection_All';

selection_Type.addEventListener('click', function (e) {
Ti.API.info( ' ==== Select Destination Hit ==== '  + e.source.id);
if (e.source.id === feedSelector)   {
            //refresh the feed view
            Ti.API.info('Simulating feed refresh for...' + e.source.id);
        }
        else {
            //reducing active label height to simulatue 2px underline
            e.source.setHeight(28);

            //reset previous selected label height
            //here's the problem
            //i know how to do this in regular javascript/html
            //but I don't know how to access an object by it's id in Titanium

           //setting current label id in feedSelector so we can change it's height next time a button is clicked
           feedSelector = e.source.id;
           Ti.API.info('Changed value for feedSelector to...' + feedSelector);
}
        }
        }

        );

        var selection_All = Ti.UI.createLabel({
        id: 'selection_All',
        text: 'All',
        width: '14.9%',
        top: 0,
        center: 2,
        height: 28,
        backgroundColor: '#fff',
        textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
        color:'#073266',
        font:{fontFamily:'Trebuchet MS',fontSize:13}

        });

    var selection_Status = Ti.UI.createLabel({
        id: 'selection_Status',
        text: 'Status',
        width: '22%',
        top: 0,
        center: 0,
        height: 30,
        backgroundColor: '#fff',
        textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
        color:'#073266',
        font:{fontFamily:'Trebuchet MS',fontSize:13}
    });

    var selection_Article = Ti.UI.createLabel({
        id: 'selection_Article',
        text: 'Article',
        width: '23%',
        top: 0,
        center: 0,
        height: 30,
        backgroundColor: '#fff',
        textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
        color:'#073266',
        font:{fontFamily:'Trebuchet MS',fontSize:13}
    });

    var selection_Video = Ti.UI.createLabel({
        id: 'selection_Video',
        text: 'Video',
        width: '20%',
        top: 0,
        center: 0,
        height: 30,
        backgroundColor: '#fff',
        textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
        color:'#073266',
        font:{fontFamily:'Trebuchet MS',fontSize:13}
    });

    var selection_Audio = Ti.UI.createLabel({
        id: 'selection_Audio',
        text: 'Audio',
        width: '20%',
        top: 0,
        center: 0,
        height: 30,
        backgroundColor: '#fff',
        textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER,
        color:'#073266',
        font:{fontFamily:'Trebuchet MS',fontSize:13}
    });

    //creating selection type container and adding selection types
    selection_Type.add(selection_All);
    selection_Type.add(selection_Status);
    selection_Type.add(selection_Article);
    selection_Type.add(selection_Video);
    selection_Type.add(selection_Audio);
    selectionView.add(selection_Type);

编辑:我使用switch语句完成了我需要做的事情,但如果我可以通过它的id来获取对象,那将会更加清晰。

var feedSelector = 'selection_All';

selection_Type.addEventListener('click', function (e) {
Ti.API.info( ' ==== Select Destination Hit ==== '  + e.source.id);
if (e.source.id === feedSelector)   {
    //refresh the feed view if the feed is open and button is clicked
    //Ti.API.info('Simulating feed refresh for...' + e.source.id);
    alert('Refreshing feed');
}
else {
    //reducing active label height to simulatue 2px underline
    e.source.setHeight(28);
    switch (feedSelector)   {
        case 'selection_All':
            selection_All.setHeight(30);
            break;
        case 'selection_Status':
            selection_Status.setHeight(30);
            break;
        case 'selection_Article':
            selection_Article.setHeight(30);
            break;
        case 'selection_Video':
            selection_Video.setHeight(30);
            break;
        case 'selection_Audio':
            selection_Audio.setHeight(30);
            break;
    }

    feedSelector = e.source.id;
    Ti.API.info('Changed value for feedSelector to...' + feedSelector);
}
}

);

1 个答案:

答案 0 :(得分:1)

如果您正在使用 Titanium Studio ,只需调试click事件并检查事件对象。在断点处停止转到表达式标签,输入e事件对象并进行检查。可能UI元素作为属性存在。

其他方式是:

var labelStore = {};


function createLabel(props){
   var label = Ti.UI.createLabel(props);
   labelStore[props.id] = label;

   return label;
}

function getLabelById(id){
   return labelStore[id];
}


var selection_Status = createLabel({
    id: 'selection_Status',
    ...
});

然后点击点击

var id = e.source.id;
var label = getLabelById(id);

....做你用标签做的事情