2013-03-25 51 views
3

我有一個使用Alloy框架構建鈦SDK 3.02的項目。 它是一個標籤的應用,我想TAB2的觀點從一個按鈕來更改內部TAB1鈦合金,需要控制器

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

解決它

設置功能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; 
... 
+0

不工作對我來說,我試圖隱藏/禁用從子視圖按鈕父視圖 – 2013-10-30 17:05:04

0

這是一種方式。 其他(恕我直言,因爲你避免全球更好)更簡單。 您只需通過做訪問標籤:

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