我是 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;