如何在Titanium中将数据从一个窗口传递到另一个窗口?

时间:2013-08-13 11:16:51

标签: titanium titanium-mobile appcelerator-titanium

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

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

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

2 个答案:

答案 0 :(得分:3)

我已经创建了一个新的合金控制器(左键单击你的项目,然后是新的),这就是我如何将参数传递给下一个视图。

新控制器称为CallBack,第一个控制器称为索引。

在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);
});

希望这有帮助。

答案 1 :(得分:0)

在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();

};

在calledWindowController.js

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

在calledWindow.js中

exports.createCalledWindow = function(title) 
{
        //Do whatever UI you want

    Ti.include('/Controllers/calledWindowController.js');
    var controller = createController(win); 

    return win;
};
相关问题