对象# & lt; Controller>没有addEventListener方法

Object #<Controller> has no method addEventListener

本文关键字:没有 addEventListener 方法 Controller lt 对象      更新时间:2023-09-26

我正在尝试创建一个控制器来为android创建一个开关按钮,因为在Titanium中没有holo寻找我需要的android,这个控制器工作正常,但是在另一个控制器中有一个addEventListener,它使用我的开关控制器,这给了我一个Object #<Controller> has no method addEventListener错误。有人告诉我,我必须在我的开关控制器中定义addEventListener方法,但我不知道如何做到这一点。什么好主意吗?

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);
    }
});

您正在尝试在容器对象上设置事件侦听器。也许你想添加事件监听器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);