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?
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 -
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);
-
Problem was I receive the event when I click on the row but nothing when I click on the switch itself….
-
I added another listener attached just for the switch and it worked!
Thanks!
-
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!