Titanium Community Questions & Answer Archive

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

Events not fired in Switch control

Hi

I have many sections in a table.
There is a section in a table row where I add a Switch.
I can't seem to capture any events from the switch at all even though I added a listener to the switch to detect for 'change' event.

There is another listener that is attached to the tableview but that too, cannot detect the switch change event.

What am I doing wrong here?

— asked March 22nd 2010 by Peter Lum
  • change
  • control
  • event
  • switch
  • table
  • view
0 Comments

5 Answers

  • Accepted Answer

    Hi Peter,

    If I add this simple code (creating table view, with table view row, with switch inside that row), I get the events just fine.

    var tv = Ti.UI.createTableView({width: 320, height: 250 });
    var tvr = Ti.UI.createTableViewRow();
    var mySwitch = Ti.UI.createSwitch( {value: false});
    mySwitch.addEventListener('change', function(e) {
        Ti.API.info('Switcheroo: ' + e.value);
    });
    
    tvr.add(mySwitch);
    tv.appendRow(tvr);
    win1.add(tv);
    

    Maybe if you posted the relevant pieces of your code, we can see in greater detail what might be going wrong for you.

    Regards,
    Bill

    — answered March 22nd 2010 by Bill Dawson
    permalink
    0 Comments
  • Here is my code:

    //
    // Select auto
    //
    data[1] = Ti.UI.createTableViewSection({
    backgroundColor:'transparent',
    color:'white',
    opacity:0.5
    });

    var switchrow = Ti.UI.createTableViewRow({
                leftImage:'../images/option_auto.png',
                backgroundColor:'transparent'
                });
    
    var label_auto = Ti.UI.createLabel({
        left:50,
        font:{fontSize:14, fontWeight:'bold'},
        color:'white',
        text:'Auto Background'
    });
    
    var sw = Ti.UI.createSwitch({
        right:10,
        value:SelectedAutoBackground // was false
    });
    
    
    
    switchrow.add(label_auto);
    switchrow.add(sw);
    
    data[1].add(switchrow);
    
    — answered March 23rd 2010 by Peter Lum
    permalink
    0 Comments
  • Problem was I receive the event when I click on the row but nothing when I click on the switch itself….

    — answered March 23rd 2010 by Peter Lum
    permalink
    0 Comments
  • I added another listener attached just for the switch and it worked!

    Thanks!

    — answered March 23rd 2010 by Peter Lum
    permalink
    1 Comment
    • Would you care to elaborate on this please.. what listener, or even better, what example code have you got to demonstrate this? I am having the same issue and don't know how to circumvent it.

      Thanks very much :)

      — commented June 21st 2011 by P Sweeney
  • Hi,

    I too faced a similar issue. I could resolve it by the order in which we add the listener.

    In my code I had created a row which had the listener on the switch and then created a tableview using this data. If we associate a listener to the switch before creating the table view and the rows we have this issue.

    So if the table view is created prior to creating the listener on the switch then the events on the switch fire neatly.

    So always create the event listeners on the switches after the creation of the table and the row elements.

    Thanks!

    — answered July 29th 2011 by Prince Nishchal
    permalink
    0 Comments
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.