对象#<controller>没有方法addEventListener </controller>

时间:2014-06-18 17:57:35

标签: javascript android titanium titanium-mobile titanium-alloy

我正在尝试创建一个控制器来为Android制作一个开关按钮,因为Titanium中的那个没有de holo寻找我需要的android,这个控制器工作正常,但是有一个addEventListener in另一个使用我的Switch控制器的控制器,它给我一个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);
    }
});

1 个答案:

答案 0 :(得分: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);