0

I am building a Titanium mobile App for iOS. I will be referring to three different screens; A home screen that has a tableview, a "master" screen that has a "search field" and a "detail" screen.

When I navigate from the home screen to the master screen, the user is presented with a search field and a message. When the user types a word into the search field, I remove the message and draw a table, then I call a PHP file on a remote server to search a database for that word and return the results as JSON and display them in that table. When the user selects one of the results a detail screen is displayed. All of that works as intended.

The problem occurs when the user returns to the master page and performs another query. I have a button that clears the table data, that button effectively clears the table. However, when the results from the second search are displayed and one is selected to see the detail page the app appears to display a detail screen but that screen continues to slide off and a second detail screen slides on and displays the data.

This same problem multiplies if the user returns to the master page and performs a third query. Again the table is populated with results but when the user selects one, TWO detail screens slide past before the user is presented with the data.

If another query is performed in the same manner as above, THREE detail screens slide past before the user is presented with the data.

This pattern continues to multiply detail screens until the user returns to the home screen. Then everything appears to reset. Here is a snippet of the code:

var win = Titanium.UI.currentWindow; 
var appsponsor = Ti.UI.createButton({});
win.add(appsponsor);
appsponsor.addEventListener('click',function(e){});

var customSearchBar = Ti.UI.createView({});

var customSearchField = Ti.UI.createTextField({});
customSearchBar.add(customSearchField);
win.add(customSearchBar);

var nolist= Ti.UI.createLabel({});
win.add(nolist);

var businessowner = Ti.UI.createLabel({});
win.add(businessowner);

var view = Ti.UI.createView({});
var table = Ti.UI.createTableView({});
view.add(table);

var message = Ti.UI.createLabel({});    

    var clear = Ti.UI.createButton({
        title: "Clear",
        style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED
        });
        clear.addEventListener("click", function() {
            message.hide();
            table.setData([]);
            tableData = [];
        }); 
Ti.UI.currentWindow.setRightNavButton(clear);

var tableData = [];

function checkInternetConnection(){
return Ti.Network.online ? true : false;
}
customSearchField.addEventListener("return", function(e) {

if(checkInternetConnection()){
    nolist.hide();
    businessowner.hide();
    win.add(view);
    var url = "http://mydomain.com/dir/file.php?title="+e.value;
    var xhr = Ti.Network.createHTTPClient({
        onload: function() {
            Ti.API.debug(this.responseText);
            var json = JSON.parse(this.responseText);
                if (json.cms_list.length< 1){ 
                    win.add(message);
                    }
                for (i = 0; i < json.cms_list.length; i++) {
                    client = json.cms_list[i];
                    row = Ti.UI.createTableViewRow({});
                        var clientlist = Ti.UI.createLabel({});
                    row.add(clientlist);
                    tableData.push(row);
                    }
                    table.addEventListener('click',function(e){
                        var row = e.row;
                        var clientlist = row.children[0];
                            var win = Ti.UI.createWindow({
                                url: 'clientdetail.js', 
                                title: clientlist.text  
                                }); 
                        var clientlist = clientlist.text;
                        win.clientlist = clientlist;
                        Ti.UI.currentTab.open(win,{animated:true});
                        }); 
                    table.setData(tableData);
        },

        onerror: function(e) {
            Ti.API.debug("STATUS: " + this.status);
            Ti.API.debug("TEXT:   " + this.responseText);
            Ti.API.debug("ERROR:  " + e.error);
            alert('There was an error retrieving the remote data. Try again.');
            },
        timeout:5000

        });

    xhr.open("GET", url);
    xhr.send();
    }
else{
    alert('Your internet connection is not available');
    }
});

Where is my error in this code that is causing it to create the problem I explained above?

dnevels
  • 397
  • 2
  • 12

1 Answers1

1

I think this is because of your click event listener.

Whenever you go back to your code and repopulate you are adding another listener. So the second time it runs the function twice, then 3 times etc.

You should either remove the listener as you click through or remove it from your onLoad function so that it doesn't get re-added every time

Martyn

Martyn Joyce
  • 304
  • 1
  • 9