【问题标题】:Titanium - Click event on view is ignoredTitanium - 视图上的点击事件被忽略
【发布时间】:2012-09-13 18:23:20
【问题描述】:

我正在开发 Titanium 并为 iOS 设备进行开发。在我的应用程序中,我有一个tableview,其中填充了由函数创建的行的部分,该函数使用从HTTPClientobject 响应接收到的数据。每行都有一个视图,在视图内有一个按钮和标签。

该按钮有一个click 事件,可以正常工作。我向存储按钮的视图添加了一个单击事件,但是当我单击视图时,不会触发该事件,但是如果我单击该按钮,则会触发按钮的事件和视图的事件。这不是它应该表现的方式,因为我希望视图的事件与按钮的事件完全不同。

为什么当我点击视图时没有触发视图的点击事件?为什么单击按钮时会触发事件?

我是这样创建行的:

function addToDo(title, priority){

    var that = {};      
    var row = Ti.UI.createTableViewRow({height:80});        
    that.currentPriority = priority;        
    that.resolveColor = function(tmpPriority){          
        switch(tmpPriority){                
            case 0: backgroundColorPriority = "#da362a"; break;
            case 1: backgroundColorPriority = "#da6c2a"; break;
            case 2: backgroundColorPriority = "#da962a"; break;
            case 3: backgroundColorPriority = "#dacb2a"; break;                         
        }               
        return backgroundColorPriority;
    }

    var rowLayout = Ti.UI.createView({
        backgroundColor : 'transparent'
    });

    var checkbox = Ti.UI.createButton({
        top: 25,
        left: 5,
        width: 30,
        height: 30,
        borderColor: 'white',
        borderWidth: 2,
        borderRadius: 1,
        backgroundColor: '#b1b1b1',
        backgroundImage: 'NONE',
        zIndex:10,
        value: false //value is a custom property in this case here.
    });
    rowLayout.add(checkbox);

    //Attach some simple on/off actions
    checkbox.on = function(item) {
        this.backgroundColor = '#62b425';

        item.currentRow.backgroundColor = "#101010";
        this.value = true;
    };

    checkbox.off = function(item) {
        this.backgroundColor = '#b1b1b1';
        item.currentRow.backgroundColor = item.resolveColor(item.currentPriority);
        this.value = false;
    };

    checkbox.addEventListener('click', function(e) {
        if(false == e.source.value) {
            e.source.on(that);
        } else {
            e.source.off(that); 
        }
    });

    // Create a Label.
    var todoTitleLabel = Ti.UI.createLabel({
        text : title,
        color : 'white',
        font : {fontSize:11},
        left : 40,
        textAlign : 'center'
    });

    // Add to the parent view.
    rowLayout.add(todoTitleLabel);

    row.add(rowLayout);

    rowLayout.addEventListener('click', function(e){
        // Whatever I put here isn't executed when I click on the rowLayout, instead I have to click the button to fire this event, that shouldn't happen
    });

    var backgroundColorPriority = that.resolveColor(that.currentPriority);      
    row.backgroundColor = backgroundColorPriority
    that.currentRow = row;
    return that.currentRow;

}

此函数在 HTTPClient onload 中调用:

var clientTask = Ti.Network.createHTTPClient({
    onload : function(e){
        var responseTask = JSON.parse(this.responseText);
        var entriesTask = responseTask.tasks;
        var todoSectionView = Ti.UI.createTableViewSection({
            headerTitle : responseTask.name
        });
        data.push(todoSectionView);
        for( var j=0; j < entriesTask.length; j++){
           var tmpRow = addToDo(entriesTask[j].name, entriesTask[j].priority);
           todoSectionView.add(tmpRow);
        }
        // add the data to the tableview
        table.data=data;
        self.updateLayout();
    },
    timeout : 60000
});

【问题讨论】:

  • 在 Ti SDK 2.1.2GA 和 iOS Simulator 5.1 上,rowLayout 点击事件对我来说运行良好,您使用的是什么 sdk 版本?

标签: ios events titanium appcelerator


【解决方案1】:

我可能会通过为 todoTitleLabel 创建一个事件处理程序或给它一个与行不同的背景颜色来测试这一点,以查看我是否能够单击行本身。也许它上面的物体挡住了它?也许您可以通过将点击事件添加到标签来完成您想要做的事情?

【讨论】:

    【解决方案2】:

    不要将事件监听器添加到行、节或行布局中。它会在 iOS 上引起问题。

    相反,将事件监听器添加到您的表格视图中,例如:

    tableView.addEventListener('click', function(e){
       var rowNumber = e.index;
       alert(rowNumber);
    });
    

    该示例将提醒所选行的编号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      • 1970-01-01
      • 2013-03-26
      • 2013-06-13
      • 2014-07-23
      • 2023-03-03
      • 2015-03-27
      相关资源
      最近更新 更多