2014-06-18 42 views
1

我想创建一个控制器来进行切换按键为Android以来,一个以钛没有去全息寻找Android的我需要,该控制器是工作的罚款没有方法的addEventListener,但有在使用该的投掷我Object #<Controller> has no method addEventListener错误我的开关控制器中的另一个控制器的addEventListener。有人告诉我,我在我的开关控制器来定义addEventListener方法,但我不知道如何做到这一点。有任何想法吗?对象#<Controller>有

customer.xml:

...  
<View> 
    <Switch id="mySwitch" platform="ios"/> 
    <Require id="mySwitch" platform="android" src="customSwitch" /> 
</View> 
... 

customer.js:

... 
$.mySwitch.addEventListener('change', function(e) { 
    // magic goes in here 
}); 
... 

customSwitch.js:

$.value = false; 

$.setValue = function(value){ 
    $.value = value; 
} 

var switchButton = Ti.UI.createButton({ 
    width     : 97, 
    height     : 24, 
    backgroundImage   : '/images/ic_switch_on.png', 
    visible     : true 
}); 

switchButton.applyProperties($.container.switchButton); 
$.container.add(switchButton); 


$.container.addEventListener('click', function(evt){ 
    $.onClick && $.onClick({}); 

    var currentValue = $.value; 

    if (currentValue) { 
     switchButton.backgroundImage = '/images/ic_switch_off.png'; 
     $.setValue(!currentValue); 
    } else { 
     switchButton.backgroundImage = '/images/ic_switch_on.png'; 
     $.setValue(currentValue); 
    } 
}); 
+0

如果低于我的答案是没有帮助的,你需要提供有关你正在尝试做一些更多的信息。在你的代码中,没有任何信息是在$ .container背后,以及你的错误信息和回溯的样子。 – daniula

回答

0

您正在尝试设置事件监听器容器对象。也许你想事件侦听器添加到您创建并添加到容器switchButton

switchButton.applyProperties($.container.switchButton); 

switchButton.addEventListener('click', function(evt){ 
    $.onClick && $.onClick({}); 

    var currentValue = $.value; 

    if (currentValue) { 
     switchButton.backgroundImage = '/images/ic_switch_off.png'; 
     $.setValue(!currentValue); 
    } else { 
     switchButton.backgroundImage = '/images/ic_switch_on.png'; 
     $.setValue(currentValue); 
    } 
}); 

$.container.add(switchButton); 
相关问题