钛合金,需要控制器

时间:2013-03-25 21:10:26

标签: javascript titanium titanium-mobile

我使用Alloy框架在钛SDK 3.02中构建了一个项目。 它是一个标签式应用程序,我想从tab1中的按钮

更改tab2的视图

tab1.xml

...
    <Button id="button" onClick="setup">
...

tab1.js

function setup(){
    //this doesn't work
    var view = Alloy.createController('tab2');
    view.changeBackground('blue');
    $.tabGroup.setActiveTab(1);
}

tab2.xml

...
    <View id="view" backgroundColor="red">
...

tab2.js

...
    exports.changeBackground = function(color){
        $.view.backgroundColor = color;
        //this runs eg
        Ti.API.info('function running');
    }

我理解为什么这不起作用。我正在创建一个永远不会添加到视图的控制器的新实例。但我想访问现有的控制器。 我试过了

var view = require('tab2');
view.changeBackground('blue');

但是这给了我一个'模块未找到错误'。 我希望这是有道理的

由于

2 个答案:

答案 0 :(得分:2)

解决了它

将tab2中的功能设置为Alloy.Global就可以了。

tab1.xml

...
    <Button id="button" onClick="setup">
...

tab1.js

function setup(){
    var changeBackgroundColor = Alloy.Globals.changeBackgroundColor;
    changeBackgroundColor('blue');
    $.tabGroup.setActiveTab(1);
}

tab2.xml

...
    var changeBackground = function(color){
        $.view.backgroundColor = color;
    }
    Alloy.Global.changeBackGroundColor = changeBackground;
...

答案 1 :(得分:0)

这是一种方式。 另一个(恕我直言,因为你避免使用Global更好)更简单。 您只需执行以下操作即可访问该标签:

function setup(){
    //This DOES work.
    // Just keep in mind that you must use the tab index corresponding to your tab.
    // Also, If your view actually uses a window and the tab is on your TabGroup view,
    // you should do $.tabGroup.tabs[1].window.
    var view = $.tabGroup.tabs[1]; 
    view.changeBackground('blue');
    $.tabGroup.setActiveTab(1);
}
相关问题