Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

how to get parent object?

app.js -> tabgroup - > tab1 - > win1(win1.js)

               - > tab2 - > win2(win2.js)

win1.js - > button

when click the button on win1, how to get tabgroup object?

I want to change setActiveTab?

app.js

  var tabGroup = Titanium.UI.createTabGroup();

  var win1 = Titanium.UI.createWindow({url:'win1.js'});

  var tab1 = Titanium.UI.createTab({  
        icon:"KS_nav_views.png",
        title:"tab 1",
        window:win1
   });

   var win2 = Titanium.UI.createWindow({url:'win2.js'}); 

   var tab2 = Titanium.UI.createTab({
    icon:"KS_nav_ui.png",
    title:"Browse",
    window: win2
   });

  tabGroup.addTab(tab1);
  tabGroup.addTab(tab2);
  tabGroup.open();

then win1.js

var curWin = Ti.UI.currentWindow;
var button = Ti.UI.createButton({title:'button'});

curWin.add(button);

button.addEventListener('click', function (e) {

    //how to get tabgroup object
    tabgroup.setActionTab(tab2);
    //
});
— asked November 22nd 2010 by becca zhang
  • parent
  • tab
  • tabgroup
  • window
0 Comments

1 Answer

  • Accepted Answer

    Becca

    In order to achieve what you want, your best option is to use a custom event, and pass json object to it that includes the tab index you would like to give focus. For example:

    app.js:

    var tabGroup = Titanium.UI.createTabGroup();
    
    var win1 = Titanium.UI.createWindow({url:'win1.js'});
    
    var tab1 = Titanium.UI.createTab({  
        icon:"KS_nav_views.png",
        title:"win 1",
        window:win1
    });
    
    var win2 = Titanium.UI.createWindow({url:'win2.js'}); 
    
    var tab2 = Titanium.UI.createTab({
        icon:"KS_nav_ui.png",
        title:"win 2",
        window: win2
    });
    
    tabGroup.addTab(tab1);
    tabGroup.addTab(tab2);
    tabGroup.open();
    
    Ti.App.addEventListener('fireEvent_setActiveTab', function(e){
        tabGroup.setActiveTab(e.tabIndex);
    });
    

    win1.js:

    var curWin = Ti.UI.currentWindow;
    
    var label = Ti.UI.createLabel({
        color:'white',
        text:'this is window 1',
        top:20
    });
    curWin.add(label);
    
    var button = Ti.UI.createButton({title:'switch to win2'});
    curWin.add(button);
    
    button.addEventListener('click', function (e) {
        Ti.App.fireEvent('fireEvent_setActiveTab', { tabIndex: 1});
    });
    

    win2.js:

    var curWin = Ti.UI.currentWindow;
    var label = Ti.UI.createLabel({
        color:'white',
        text:'this is window 2',
        top:20
    });
    curWin.add(label);
    
    var button = Ti.UI.createButton({title:'switch to win1'});
    curWin.add(button);
    
    button.addEventListener('click', function (e) {
        Ti.App.fireEvent('fireEvent_setActiveTab', { tabIndex: 0});
    });
    

    Note: this code has been tested and will work without modification.

    Here, I've attached the custom event to Ti.App. You could attach it to a view or window, but this is currently not available for android 1.4.X (although it is in 1.5.X). The advantage of attaching it to a window/view, is that it is torn down with then view is destroyed whereas events of Ti.App are persistent throughout the app's instance (one session of the app). In this case, as you are using a tabgroup, and a tabgroup is generally present throughout the app instance lifetime, then I don't think it's a concern.

    Hope this helps

    — answered November 22nd 2010 by Paul Dowsett
    permalink
    1 Comment
    • Piece of cake. never got the idea. Now i love fireEvents. Your answer saved me after 3 years. :)

      — commented December 20th 2013 by Shiva Kumar Avula
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.