-1

I have the following setup: On ApplicationTabGroup.js I have a tabgroup

var tabGroup = Ti.UI.createTabGroup(); with 2 tabs.

One of those tabs calls an external URL, here is the code

//Restaurant Window
    var restaurant = Titanium.UI.createWindow({
        tabBarHidden:true,
        color: '#FFF',
        url: 'restaurants.js',
        navBarHidden:false,
        statusBarStyle: Titanium.UI.iPhone.StatusBar.TRANSLUCENT_BLACK
    });


    var tab2 = Ti.UI.createTab({
        title:"x",
        color: '#FFF',
        backgroundColor:'#00c0f3',
        window: restaurant
    });

And I open that tab with an EventListener like this

tabGroup.setActiveTab(tab2).open();

The problem is that from restaurant.js I don't know how to get back to the first tab, or any other tab because the tabgroup in restaurant.js is not defined.

How can I navigate through tabs that are called in different URLs when the tabgroup is just defined in ApplicationTabGroup.js?

I am using - iOS7 - SDK 3.1.3 GA

Any tip in the right direction will be much appreciated!

4

1 回答 1

3

出于这个原因,您真的不应该使用该url属性,因为它会产生完全不同的 JS 上下文,相反,您应该将选项卡窗口 (restaurants.js) 包装在 CommonJS 样式模块中。

但是,如果您不想使这个模块化,您应该能够将 tabGroup 附加为窗口的属性,如下所示:

var restaurant = Titanium.UI.createWindow({
    tabBarHidden:true,
    color: '#FFF',
    url: 'restaurants.js',
    navBarHidden:false,
    statusBarStyle: Titanium.UI.iPhone.StatusBar.TRANSLUCENT_BLACK
});
restaurant.myTabGroup = tabGroup;

然后你可以导航到你的restaurants.js文件中的另一个标签:

// Set active tab to the third tab
Titanium.UI.currentWindow.myTabGroup.setActiveTab(3).open();

不过,我强烈建议您进行模块溃败。

模块

编辑:这是使用寄生继承模块化窗口的尝试。请查看本指南以获取更多详细信息。

function MyWindow(parentTabGroup) {
    var restaurant = Titanium.UI.createWindow({
        tabBarHidden:true,
        color: '#FFF',
        navBarHidden:false,
        statusBarStyle: Titanium.UI.iPhone.StatusBar.TRANSLUCENT_BLACK
    });

    // Here is where you add all the controls and views you had put inside  
    // 'restaurant.js'.....
    // Also, if you have an event listener you can call the tab group
    button.addEventListener('click', function(e) {
         parentTabGroup.setActiveTab(3);
    });
    return restaurant;
}
module.exports = MyWindow;

因此,现在您可以创建窗口并将其添加到选项卡组,如下所示:

var MyWindow = require('MyWindow');
var mywin = new MyWindow(tabGroup);
var tab2 = Ti.UI.createTab({
    title:"x",
    color: '#FFF',
    backgroundColor:'#00c0f3',
    window: mywin
});
于 2013-10-03T19:33:08.667 回答