2012-05-11 52 views
1

我在製作一個使用普通JS的可滾動菜單。Titanium mobile - 視圖上的addEventListener

菜單項是包含其他兩個組件的視圖: 圖標的imageView以及此菜單的文本標籤。

在android和ios模擬器上,comportement很奇怪,而且不一樣。

在android上,如果點擊是在標籤上或在imageview上完成的,則會給出:「未捕獲的TypeError:無法讀取屬性...」 在iphone上,該程序不會啓動任何內容。

如果我點擊其他地方(仍然進入查看項目),但不是在圖像上或在拉巴爾,例如在邊緣,這是完美的作品!

這裏是代碼:

功能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 =要求( '視圖/模塊/ menuIconsItem');):

功能menuIconsItem(項目){

// 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;

回答

2

您還必須爲標籤和圖像視圖設置標識。

+0

你這個石頭夥計,現在工作很完美! 很好的幫助,非常感謝你! – geoffrey

相關問題