1

我是 Titanium 的新手,正在为 iPhone 创建我的虚拟应用程序。我正在使用教程中的这段代码。但是我被困在“itemSelected”事件监听器中,看起来在这里不起作用。我尝试添加按钮、标签和它们的点击事件,即使它们不工作。我在这里没有得到我的错误。所以请指教。提前致谢。

我的 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;
4

2 回答 2

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;
    }
})
于 2017-02-27T10:06:49.293 回答
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 });
});

这会成功的

您也可以参考什么是 fireevent 我如何使用它

于 2013-09-27T04:21:17.913 回答