【问题标题】:How to pass data from one window to another in Titanium?如何在 Titanium 中将数据从一个窗口传递到另一个窗口?
【发布时间】:2013-08-13 11:16:50
【问题描述】:

我想将数据从一个窗口发送到另一个窗口。

示例: 我在第一个窗口上有一个文本字段和一个按钮。单击窗口时,我需要将文本字段值发送到第二个窗口?

我找到了一个教程,但它不在 MVC 中。

【问题讨论】:

    标签: titanium titanium-mobile appcelerator-titanium


    【解决方案1】:

    我创建了一个新的合金控制器(在您的项目中单击鼠标左键然后新建),这是我将参数传递给下一个视图的方式。

    新的控制器叫做CallBack,第一个控制器叫做index。

    在 CallBack.xml 我有:

    <Alloy>
        <View class="container">
        </View>
    </Alloy>
    

    在 CallBack.tss 我有:

    ".container": {
        backgroundColor: "black"
    }
    

    在 CallBack.js 我有:

    var args = arguments[0] || {};
    //here you can do whatever you want to your parameter, i just show the value.
    alert(args.textField);
    

    最后在 index.js 中,这就是我传递 textField 参数的方式:

    //with a button i can open a new view in my current window
    $.btnNext.addEventListener('click',function(e){
        //tfInsert is the id of my textfield in index.xml file and with .value i can access to whatever it contains
        //the "?" operator is like an if
        var textField = $.tfInsert.value != "" ? textField = $.tfInsert.value : textField = "Hello";
        var nextView = Alloy.createController('/CallBack', {
            textField: textField
        }).getView();
        //this is how i add a new view to my current window
        $.window.add(nextView);
    });
    

    希望这会有所帮助。

    【讨论】:

    • 我不需要使用Alloy。我需要使用common.js
    【解决方案2】:

    在 controller.js 中(我们从中传递数据)

    function createController(win)
     {
        //title is the data to pass
        var platform = Ti.Platform.osname;
        var calledWindow = require('/ui/' + platform + '/addProductWindow').createCalledWindow(title);
            calledWindow.open();
    
    };
    

    在被调用的WindowController.js中

    function createController(win){
        //Do whatever control you want
    };
    

    在被调用的Window.js中

    exports.createCalledWindow = function(title) 
    {
            //Do whatever UI you want
    
        Ti.include('/Controllers/calledWindowController.js');
        var controller = createController(win); 
    
        return win;
    };
    

    【讨论】:

    • 我听不懂,请您解释一下您的答案文件结构或将其添加到答案中吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多