Titanium EventListener不起作用

时间:2013-09-26 12:45:08

标签: iphone titanium

我是Titanium的新手,并为iPhone创建我的虚拟应用程序。 我在教程中使用了这段代码。 但我陷入'itemSelected'eventListener,看起来不在这里工作。 我试过添加按钮,标签和他们的点击事件,即使他们不工作。 我在这里没有弄错。 所以请建议。 提前谢谢。

我的ApplicationWindow.js是..

//Application Window Component Constructor

function ApplicationWindow() {
//declare module dependencies
var MasterView = require('ui/listView_common/MasterView'),
    DetailView = require('ui/listView_common/DetailView');

//create object instance
var self = Ti.UI.createWindow({
    backgroundColor:'#fff'
});

//construct UI
var masterView = new MasterView(),
    detailView = new DetailView();

//create master view container
var masterContainerWindow = Ti.UI.createWindow({title:'List View'});
masterContainerWindow.add(masterView);

//create detail view container
var detailContainerWindow = Ti.UI.createWindow({left:100,title:'Detail View'});
detailContainerWindow.add(detailView);

//create iOS specific NavGroup UI
var navGroup = Ti.UI.iPhone.createNavigationGroup({
    window:masterContainerWindow
});
self.add(navGroup);

//add behavior for master view
masterView.addEventListener('itemSelected', function(e) {
    alert("Alert");
    navGroup.open(detailContainerWindow);
    detailView.showArticle(e.link);
});

function refreshData() {
    var file = Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory+'jsonFiles/data.json');
    var data = file.read().text;
    var json = JSON.parse(data);
    masterView.refreshDataTable(json);
}

// load data
refreshData();

return self;
};
module.exports = ApplicationWindow;

我的'MasterView.js'是......

var createRow = function(item) {

var tablerow = Ti.UI.createTableViewRow({
    height: 90,
    link: item.link,
    className: 'itemRow',
    hasChild: true
});
var imageview = Ti.UI.createImageView({
    image: item.image,
    height: 55, 
    width: 68, 
    left: 5,
    top: 3
});
var titleview = Ti.UI.createLabel({
    text: item.title,
    color: '#000',
    font: {
        fontSize: 16
    },
    left: 83,
    right: 5,
    top:5,
    width:300
});
var dateview = Ti.UI.createLabel({
    text: item.pubDate,
    textAlign: 'center',
    color: '#444',
    font: {
        fontSize: 12    
    },
    height: 'auto',
    width: 68,
    left: 5,
    top: 60
});
var nameview = Ti.UI.createLabel({
    text: item.firstName +" " + item.lastName,
    color: '#000',
    font: {
        fontSize: 14
    },
    left: 83,
    right: 5,
    top:30
});
var descriptionview = Ti.UI.createLabel({
    text: item.description,
    color: '#000',
    font: {
        fontSize: 12
    },
    left: 83,
    top:50
});
tablerow.add(imageview);
tablerow.add(dateview);
tablerow.add(titleview);
tablerow.add(nameview);
tablerow.add(descriptionview);

return tablerow;
};

//Master View Component Constructor
function MasterView() {
var self = Ti.UI.createView({
    backgroundColor:'#fff'
});

var table = Ti.UI.createTableView();
self.add(table);
table.addEventListener('click', function(e) {
    self.fireEvent('itemSelected', { link: e.row.link });
});

self.refreshDataTable = function(data) {
    if (Object.prototype.toString.apply(data) === '[object Array]') {
        var rows = [];
        for (var i = 0; i < data.length; i++) {
            rows.push(createRow(data[i]));
        }
        table.setData(rows);
    }
};

return self;
}

module.exports = MasterView;

DetailView.js

//Detail View Component Constructor
function DetailView() {
var self = Ti.UI.createView();
var webview = Ti.UI.createWebView();
self.add(webview);

self.showArticle = function(url) {
    webview.url = url;
};

webview.addEventListener('load', function(e) {
    self.fireEvent('articleLoaded');
});

return self;
}
module.exports = DetailView;

2 个答案:

答案 0 :(得分:1)

itemSelected不是窗口的事件类型。这就是为什么它无法正常工作的原因。 尝试点击事件并获取apiName并根据apiName执行您的工作。

示例:

windowNAme.addEventListener('click',function(e){
    //when we click on button then we get id from this 
    getId=e.source.id;
    if(getId=="mybutton")
    {
        perform your action here;
    }
})

答案 1 :(得分:0)

尝试在代码中使用Titanium.App.addEventListener,如下所示

//add behavior for master view 
  Ti.App.addEventListener('itemSelected', function(e) {
    alert("Alert");
    navGroup.open(detailContainerWindow);
    detailView.showArticle(e.link);
  });

并点击事件

table.addEventListener('click', function(e) {
    Ti.App.fireEvent('itemSelected', { link: e.row.link });
});

这样就可以了解

您也可以参考what is fireevent how do i use it