Titanium Community Questions & Answer Archive

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

Buttonbar to windows

Hello All,
I am a newbie and I have what is probably an easy question. I am trying to have the buttons in a buttonbar open different .js files. Here is my code:

var win = Titanium.UI.createWindow({
   backgroundColor:'#47C1C1' 
});

win.orientationModes = [
        Titanium.UI.PORTRAIT,
        Titanium.UI.LANDSCAPE_LEFT,
        Titanium.UI.LANDSCAPE_RIGHT
    ];

win.open({fullscreen:true});

//
// CUSTOM BUTTON BAR
// 

var buttonObjects = [
    {image:'images/med_button_sm.png', width:60},
    {image:'images/sld_button_sm.png', width:60},
    {image:'images/tlr_button_sm.png', width:60},
    {image:'images/res_button_sm.png', width:60},
    {image:'images/pub_button_sm.png', width:60}
];

var bb1 = Titanium.UI.createButtonBar({
    labels:buttonObjects,
    backgroundColor:'#47C1C1',
    top:390,
    style:Titanium.UI.iPhone.SystemButtonStyle.BAR,
    height:60,
    width:'auto'
});

win.add(bb1);


bb1.addEventListener('click', function(e)

{
    // button 1 - med button

var win1 = Titanium.UI.createWindow({
    backgroundColor:'#47C1C1',
    url:'meditations.js',
    title:'Meditations'
});

win1.orientationModes = [
        Titanium.UI.PORTRAIT,
        Titanium.UI.LANDSCAPE_LEFT,
        Titanium.UI.LANDSCAPE_RIGHT
    ];

var b1 = Titanium.UI.createButton({
        title:'Close',
        style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
    });
    win1.setLeftNavButton(b1);
    b1.addEventListener('click',function()
    {
        win1.close();
    });

win1.open({modal:true});

    // button 2 - slideshow

var win2 = Titanium.UI.createWindow({  
    backgroundColor:'#47C1C1',
    url:'slideshow.js',
    title:'Rose Slideshow'
});

win2.orientationModes = [
        Titanium.UI.PORTRAIT,
        Titanium.UI.LANDSCAPE_LEFT,
        Titanium.UI.LANDSCAPE_RIGHT
    ];
var b2 = Titanium.UI.createButton({
        title:'Close',
        style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
    });
    win2.setLeftNavButton(b2);
    b2.addEventListener('click',function()
    {
        win2.close();
    });

win2.open({modal:true});

//button 3 - truth/lie

    var win3 = Titanium.UI.createWindow({  
    backgroundColor:'#47C1C1',
    url:'truthlie.js',
    title:'A Rose'
});

win3.orientationModes = [
        Titanium.UI.PORTRAIT,
        Titanium.UI.LANDSCAPE_LEFT,
        Titanium.UI.LANDSCAPE_RIGHT
    ];

var b3 = Titanium.UI.createButton({
        title:'Close',
        style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
    });
    win3.setLeftNavButton(b3);
    b3.addEventListener('click',function()
    {
        win3.close();
    });


win3.open({modal:true});

//button 4 - resources

var win4 = Titanium.UI.createWindow({  
    backgroundColor:'#47C1C1',
    url:'resources.js',
    title:'Meditation Resources'
});

win4.orientationModes = [
        Titanium.UI.PORTRAIT,
        Titanium.UI.LANDSCAPE_LEFT,
        Titanium.UI.LANDSCAPE_RIGHT
    ];

var b4 = Titanium.UI.createButton({
        title:'Close',
        style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
    });
    win4.setLeftNavButton(b4);
    b4.addEventListener('click',function()
    {
        win4.close();
    });

win4.open({modal:true});


//button 5 - publications

var win5 = Titanium.UI.createWindow({  
    backgroundColor:'#47C1C1',
    url:'publications.js',
    title:'Publications'
});

win5.orientationModes = [
        Titanium.UI.PORTRAIT,
        Titanium.UI.LANDSCAPE_LEFT,
        Titanium.UI.LANDSCAPE_RIGHT
    ];

var b5 = Titanium.UI.createButton({
        title:'Close',
        style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
    });
    win5.setLeftNavButton(b5);
    b5.addEventListener('click',function()
    {
        win5.close();
    });

win5.open({modal:true});


});

Thanks in advance.

— asked August 1st 2010 by Louis Crockett
  • buttonbar
0 Comments

1 Answer

  • Accepted Answer

    Inside your click event handler, check the e.index property. First button's index is 0 and so on.

    For example:

    bb1.addEventListener('click', function(e) {
        if (e.index === 0) {
            // open window 1
        } else if (e.index === 1) {
            // open window 2
        } else ...
    }
    
    — answered August 1st 2010 by Goran Skledar
    permalink
    1 Comment
    • Thank u so much
      this is what i was looking for
      u r imazing

      — commented September 24th 2013 by Mohammed Abunada
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.