【问题标题】:titanium alloy open view from button钛合金按钮打开视图
【发布时间】:2013-11-05 20:48:53
【问题描述】:

在离开 Titanium 一段时间后,我似乎甚至忘记了最简单的任务。举个例子:我在主页上有一堆按钮。单击时,每个按钮都应打开其各自的视图。单击按钮时如何打开该关联视图?

查看

<Alloy>
<Tab title="Home">
    <Window class="container">
        <Label>This is the Home View</Label>
        <ImageView id="kplLogo"/>
        <View id="homeNav">
            <Button class="navButton" id="recognizeButton" title="Recognize" onClick="doClick" />
            <Button class="navButton" id="reactButton" title="React"/>
            <Button class="navButton" id="reportButton" title="Report"/>

        </View>
    </Window>
</Tab>
</Alloy>

当用户点击一个按钮时,比如现在的识别按钮,它应该打开识别视图。我知道这很简单,但我现在脑子有问题。

感谢您的帮助。询问您是否需要更多详细信息。

【问题讨论】:

    标签: titanium-alloy


    【解决方案1】:

    首先,为每个 Button 添加一个属性,以便调用子 View 控制器,例如:

    <Button id="recognizeButton" title="Recognize" child_controller="recognizeView" />
    

    同时在&lt;tab&gt; 元素上使用 id:

    <Tab id="hometab">
    

    然后,在控制器中,添加事件监听器:

    $.recognizeButton.addEventListener('click', function(e) {
        if (e.source.child_controller) {
          controller = Alloy.createController(e.source.child_controller);
          $.hometab.open(controller.getView());
        }
    });
    

    这将在同一选项卡中打开一个新窗口,保留历史记录,以便当您单击 reutrn 时,您将返回主选项卡。如果您需要更广泛的示例,请检查:https://github.com/asiviero/drupanium_app/tree/master/app 我以这种方式使用主视图,从“包含”文件夹中的控制器打开视图

    【讨论】:

    • 感谢您的回复。我按原样尝试了代码,但是当我单击按钮时没有任何反应。我在侦听器中发出警报,但它从未触发。至少我没有收到错误。因此,已经取得了一些进展。谢谢。如果您有任何进一步的建议,请告诉我。
    • 如果您还没有添加任何样式,可能是您没有点击右键,因为它们已经堆积如山了。您是否尝试过使用单个按钮而不是所有按钮?
    • 发现了问题,我正在编辑我的原始答案以修复它。基本上它需要从 e.source 询问 child_controller 而不是纯粹的 e.
    • 它更接近了,但还不完全正确。这一行 $.homeTab.open(controller.getView());触发错误 Invalid type passing to function in homeView.js(第 83 行)。所以它不喜欢将 getView() 作为参数。
    • 澄清一下,Ti.Ui.Tab.open() 接收一个Window 对象作为参数,并在渲染堆栈顶部打开它。如果您希望通过 Window 对象执行相同操作,请查看:gist.github.com/asiviero/7347533
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    相关资源
    最近更新 更多