Guest User

Untitled

a guest
Jan 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. var CustomSwitch = function(onText, offText, value) {
  2. // the custom view
  3. var view = Ti.UI.createView();
  4.  
  5. var label = Ti.UI.createLabel();
  6. var sw = Ti.UI.createSwitch();
  7.  
  8. view.add(label);
  9. view.add(sw);
  10.  
  11. // this function returns the text to be shown in the label, based on val
  12. var getLabelText = function(val) {
  13. return (val ? onText : offText);
  14. };
  15.  
  16. // this function updates the label and the switch based on val
  17. var changeValue = function(val) {
  18. label.text = getLabelText(val);
  19. sw.value = val;
  20. };
  21.  
  22. // when the switch changes its state we change the label and fire and event to the listeners
  23. sw.addEventListener('change', function(e) {
  24. changeValue(e.value);
  25. view.fireEvent('change', e);
  26. });
  27.  
  28. //initialize the state of our custom view
  29. changeValue(value);
  30.  
  31. //Privileged method
  32. view.changeValue = changeValue;
  33.  
  34. return view;
  35. };
  36.  
  37. // we instantiate it as:
  38. var my_switch = new CustomSwitch('switched on', 'switched off', true);
  39.  
  40. //my_switch is now a reference to the view created in the CustomSwitch() function
  41.  
  42. Ti.UI.currentWindow.add(my_switch);
Add Comment
Please, Sign In to add comment