【问题标题】:Titanium mobile - addEventListener on a viewTitanium mobile - 视图上的 addEventListener
【发布时间】:2012-05-11 12:26:25
【问题描述】:

我正在使用普通的 JS 制作一个可滚动的菜单。

菜单项是包含其他 2 个组件的视图: 图标的 imageView 和此菜单文本的标签。

在android和ios模拟器上的行为很奇怪,不一样。

在 android 上,如果在 label 或 imageview 上单击,则会给出:“uncaught TypeError: Cannot Read property...” 在 iphone 上,这只是不启动任何东西。

如果我点击其他地方(仍然进入视图项目)但不是在图像或唇上,例如在边缘上,那就完美了!!

代码如下:

function menuIcons(itemTab) {

var menuMain = Ti.UI.createView({
    layout : 'vertical',
    backgroundColor : '#333333',
    height : 125,
    bottom : 10,
    left : 10,
    right : 10,
    borderRadius : 5.0
});

var menuFirstLine = Ti.UI.createScrollView({
    scrollType : 'horizontal',
    contentHeight : 120,
    contentWidth : 'auto',
    layout : 'horizontal',
    height : 120,
    marginLeft : 5
});

var items = [];
var menuIconsItem = require('view/module/menuIconsItem');

for(var i in itemTab) {
    var page = itemTab[i].page;

    items[i] = new menuIconsItem(itemTab[i]);

    (function(itemsEvent) {
        itemsEvent.id = itemTab[i].id;
        itemsEvent.addEventListener('click', function(e) {

            Ti.App.fireEvent('main_menu_' + itemsEvent.id, {
                id : e.source.id
            });
        })
    })(items[i]);

    menuFirstLine.add(items[i]);
}
menuMain.add(menuFirstLine);
return menuMain;

}

module.exports = menuIcons;

以及所需项目的代码(var menuIconsItem = require('view/module/menuIconsItem');):

function menuIconsItem(item) {

// path for images on Android  besoin de centraliser tout ca
var pathImages = '';

var itemImage = Ti.UI.createImageView({
    image : item.imageLink,
    width : 64,
    height : 64,
    top : 15
});

var itemLabel = Ti.UI.createLabel({
    color : '#afafaf',
    text : item.text,
    font : {
        textAlign : 'center'
    },
    height : 40,
    top : 80
});

var menuItem = Ti.UI.createView({
    width : 120,
    height : 120,
    backgroundColor : '#424242',
    top : 5,
    left : 5
});

menuItem.add(itemImage);
menuItem.add(itemLabel);

return menuItem;

}

module.exports = menuIconsItem;

【问题讨论】:

    标签: events view titanium titanium-mobile addeventlistener


    【解决方案1】:

    您还必须为标签和图像视图设置 id。

    【讨论】:

    • 你太棒了,伙计,现在完美运行!帮助很大,非常感谢!
    猜你喜欢
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2011-07-31
    相关资源
    最近更新 更多