on/off createSwitch - 1st click doesnt change text

You must Login before you can answer or comment on any questions.

i'm showing/hiding mapview routes but when i implement this the first click removes the route but text stays at "off" - subsequent clicks toggle the route and text but the text is now 1 step behind ???

(i tried this with an imageview & the same thing happens)

var routeSwitch = Titanium.UI.createSwitch({
  top:40,
  left:0,
  value:false,
  visible:false
});
container.add(routeSwitch);
 
var routeCheckbox = Titanium.UI.createLabel({
  text:'off',
  width:50,
  height:50,
  top:40,
  left:10,
  color:'#fff'
});
 
 
routeCheckbox.addEventListener('click',function(e) {
  if(routeSwitch.value==false) {
    routeSwitch.value = true;
    routeCheckbox.text = 'off';
 
    mapview.removeRoute(route); 
 
  } else {
    routeSwitch.value = false;
    routeCheckbox.text = 'on';
    mapview.addRoute(route);    
  }
});
 
container.add(routeCheckbox);

— asked 8 months ago by adrian harris
1 Comment
  • dumb ejit: i ended up adding in the routeCheckbox.text to the listener that opens the container and swapping the text around in the routeCheckbox listener

    this works for anyone stumbling across this

    var r = Titanium.UI.createButton({
        systemButton:Titanium.UI.iPhone.SystemButton.INFO_LIGHT
    });
    r.addEventListener('click',function(){
    container.animate({right:4, duration:500});
    mapview.animate({right:100, duration:500});
    routeCheckbox.text = 'off';
    });
    win.setRightNavButton(r);
     
     
     
     
            var container = Ti.UI.createView({
            borderWidth:1,
            borderColor:'#777',
            borderRadius:4,
            backgroundColor:'#03054d',
            //backgroundImage:'images/container-bg.png',
            width:100,
            top:0,
            height:220,
            right:-190
        });
            win.add(container);
     
     
    var routeSwitch = Titanium.UI.createSwitch({
      top:40,
      left:0,
      value:false,
      visible:false
    });
    container.add(routeSwitch);
     
    var routeCheckbox = Titanium.UI.createLabel({
      text:'',
      width:50,
      height:50,
      top:40,
      left:10,
      color:'#fff'
    });
     
     
    routeCheckbox.addEventListener('click',function(e) {
      if(routeSwitch.value==false) {
        routeSwitch.value = true;
        routeCheckbox.text = 'on';
     
        mapview.removeRoute(route); 
     
      } else {
        routeSwitch.value = false;
        routeCheckbox.text = 'off';
        mapview.addRoute(route);    
      }
    });
     
    container.add(routeCheckbox);

    — commented 8 months ago by adrian harris

Your Answer

Think you can help? Login to answer this question!