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 and TabbedBar 'click' event listeners are clunky

So the way ButtonBar, TabbedBar, OptionDialog, etc work is you set up a click listener on the bar itself, and then you use e.index to determine which button was pressed.

This is a clunky method and difficult to maintain. If you insert or remove a button from the bar (god forbid you do it dynamically!), your event listener gets confused unless you update it accordingly. I have taken to doing something more like this:

buttonbar.addEventListener('click', function(e) {
  if (e.source.labels[e.index].title.match(/add/i)) {
    // add something
  } else if (e.source.labels[e.index].title.match(/delete/i)) {
    // delete something
  }
});

This is a pretty good method but I'd like to see title and image added directly to the callback parameter. So my example could become:

buttonbar.addEventListener('click', function(e) {
  if (e.title.match(/add/i)) {
    // add something
  } else if (e.title.match(/delete/i)) {
    // delete something
  }
});

Alternatively you could come up with a way to add event listeners to individual buttons in the bar.

— asked May 17th 2010 by Nick Wing
  • buttonbar
  • mobile
  • optiondialog
  • tabbedbar
0 Comments

0 Answers

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.