event listener on button not working

You must Login before you can answer or comment on any questions.

Hi my event listener on my button (cButton) is not working and i don't understand why this is my code is as follows:

var win1 = Ti.UI.createWindow();
    var cButton = Titanium.UI.createButton({
        title : 'close',
        top : 10,
        width : 50,
        height : 20
    });
    cButton.addEventListener('click', function(event) {
        alert("in eventListener");
        win1.close();
    });
 
    tableview.addEventListener('click', function(event) {
 
        Ti.UI.backgroundColor = 'white';
 
        var image = Ti.UI.createImageView({
            image : event.rowData.identifier
        });
 
        win1.add(cButton);
        win1.add(image);
        win1.open();
    });
Any Ideas?

— asked 9 months ago by Michael Sagar
2 Comments
  • Missing from your question are a number of important pieces of information. I suggest you take a look at the Using Questions and Answers article, specifically the Q&A Question Checklist. The missing information is critical to reproducing problems in a test environment and often indicates other factors that cause the undesirable outcome you are experiencing.

    — commented 9 months ago by Stephen Feather

  • apologies the lines above the original posted code is as follows:

    var tableview = Titanium.UI.createTableView({
            data : data,
            minRowHeight : 58
        });
    There are no other bits of code which effect this.

    — commented 9 months ago by Michael Sagar

2 Answers

Accepted Answer

Without a complete minimal case, it's a bit hard to say what's wrong.

I would try restructuring your code so that you construct win1 inside the tableview's click event listener.

tableview.addEventListener('click', function(event) {
        var win1 = Ti.UI.createWindow({
            backgroundColor: '#fff';
        );
 
        var cButton = Titanium.UI.createButton({
            title : 'close',
            top : 10,
            width : 50,
            height : 20
        });
 
        cButton.addEventListener('click', function(event) {
            alert("in eventListener");
            win1.close();
        });
 
        var image = Ti.UI.createImageView({
            image : event.rowData.identifier
        });
 
        win1.add(cButton);
        win1.add(image);
        win1.open();
    });
Even better would be to build a custom window class in a CommonJS module and instantiate a new custom window, passing it the image you want to display.

just move where you add the button and image to win1 outside of the tableview event listener and just open win1 from the tableview event listener and it will work.

Your Answer

Think you can help? Login to answer this question!