【发布时间】:2014-12-19 16:36:54
【问题描述】:
首先:我正在练习使用钛,这只是我第一个对框架充满信心的“Hello World”应用程序,所以任何建议都会非常感谢。
我有这个简单的看法:
<ScrollView id="grid" dataCollection="pictures">
<View class="single-item" title="{title}" author="{author}" desc="{desc}" onClick="showPic">
<ImageView class="thumb" image="/images/thumb-stock-1.jpg" />
<Label class="title" text="{title} by {author}" />
<Label class="desc" text="{desc}" />
</View>
</ScrollView>
单击每个项目,我都会调用控制器中定义的函数 showPic(),这没关系。但我想将一些参数传递给该函数,即 {title}、{author} 和 {desc},以便我可以处理它们并将它们“打印”到每个特定项目的新详细视图上。使用 TableView 很容易(我只是将:event.source -> title, event.source -> author ... 放在我的控制器中,我可以读取该表行数据),但我的观点似乎不起作用。
所以我的问题是: 1)如何将参数从 VIEW: 传递到 CONTROLLER showPic() 2)一般来说,如果有更好的视图来列出一些对象并单击打开每个对象,请告诉我如何了解更多内容:D(PS:我不能使用 tableView,因为我的布局不适合这种观点)
---- 编辑:下面是我的完整代码
index.xml:
<Alloy>
<Collection src="pictures"/>
<NavigationWindow id="navGroupWin">
<Window class="container" title="La mia galleria">
<View class="arrow arrow-up"><Label text="UP" /></View>
<ScrollView id="grid" dataCollection="pictures">
<View class="single-item" title="{title}" author="{author}" desc="{desc}" onClick="showPic">
<ImageView class="thumb" image="/images/thumb-stock-1.jpg" />
<Label class="title" text="{title} by {author}" />
<Label class="desc" text="{desc}" />
</View>
</ScrollView>
<View class="arrow arrow-down"><Label text="DOWN" /></View>
</Window>
</NavigationWindow>
</Alloy>
来自 index.js 的显示图片:
function showPic(event) {
var pic = event.source;
var args = {
title: pic.title,
desc: pic.desc,
author: pic.author
};
var picView = Alloy.createController("detail", args).getView();
if (OS_IOS) {$.navGroupWin.openWindow(picView);}
if (OS_ANDROID) {picView.open();}
}
detail.xml:
<Alloy>
<Window class="container">
<View layout='vertical'>
<Label id="titleLabel"></Label>
<Label id="descLabel"></Label>
<Label id="authorLabel"></Label>
</View>
</Window>
</Alloy>
detail.js
var args = arguments[0] || {};
$.titleLabel.text = args.title || 'Default Title';
$.descLabel.text = args.desc || 'Default desc';
$.authorLabel.text = args.author || 'Default author';
当我点击索引的每个项目时,我的 source.title、source.author 和 source.desc 似乎是空的,并且在详细信息窗口中我只返回了“默认”值
【问题讨论】:
-
你能提供你的js代码吗? e.source 应该可以正常工作
-
谢谢,我用完整的代码编辑了我的第一篇文章!
标签: view controller titanium titanium-alloy