【问题标题】:Changing Active Tab From a Tab in Different URL?从不同 URL 中的选项卡更改活动选项卡?
【发布时间】:2013-10-03 15:48:59
【问题描述】:

我有以下设置:在ApplicationTabGroup.js 我有一个标签组

var tabGroup = Ti.UI.createTabGroup(); 带有 2 个标签。

其中一个选项卡调用外部 URL,这是代码

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

然后我用这样的 EventListener 打开该选项卡

tabGroup.setActiveTab(tab2).open();

问题是从restaurant.js 我不知道如何返回到第一个选项卡或任何其他选项卡,因为restaurant.js 中的tabgroup 未定义。

当标签组刚刚在ApplicationTabGroup.js 中定义时,如何浏览在不同 URL 中调用的标签?

我正在使用 - iOS7 - SDK 3.1.3 GA

任何正确方向的提示将不胜感激!

【问题讨论】:

  • Downvoter 很抱歉让您度过了糟糕的一天。希望现在一切都好。

标签: tabs titanium ios7 appcelerator appcelerator-mobile


【解决方案1】:

出于这个原因,您真的不应该使用 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();

不过,我强烈建议你去模块溃败。

模块

编辑:这是使用寄生继承模块化窗口的尝试。看看this guide for more detail.

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

【讨论】:

  • 谢谢 Josiah,我正在尝试按照您的建议走模块路线(尽管到目前为止我遇到了各种各样的错误)
  • 我编辑了答案以提供更多指导,希望对您有所帮助!
  • 由于某种有线原因,在实施模块化版本后,我在使用tabGroup.open(); 打开 tabGroup 的那一行得到了Invalid type passed to function,知道为什么会发生这种情况吗?提前致谢!
  • 忽略我最后的评论我对 tabGroup 的执行顺序有疑问。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 2016-09-29
  • 1970-01-01
  • 2018-03-26
  • 2018-06-10
相关资源
最近更新 更多