Hello all,
I am trying to make an app in which the user clicks a button and a question is given to the user in the form of a label.
var win1 = Titanium.UI.createWindow({ backgroundColor : 'White' }); var questionPresent = 0; // variable that determines whether the question is already present on the screen. 1 means present, 0 means not present var openButton = Titanium.UI.createButton({//button used to open question width : 'auto', height : 'auto', top : 100, left : 0, title : 'open the test label!' }); openButton.addEventListener('click', function(e) { if (questionPresent == 0) {//if no question is present, create the question label var question = Titanium.UI.createLabel({ top : 300, left : 150, width : 'auto', height : 'auto', text : 'Question is here.' }); win1.add(question); //adds the question to the view questionPresent = questionPresent = 1; //updates the "questionPresent" value, meaning question is now present. } else if (questionPresent == 1) {//if question is present, remove it. this is where the error occurs win1.remove(question) } }); win1.add(openButton); win1.open(); });The problem is that when I try to remove the label from the window, it gives me an error: "Invalid value, expected type object." Please help! What am I doing wrong?
1 Answer
your "var question = Titanium.UI.createLabel({}) will overwrite on the second time clicked because you declared it in button click.
you can try this.
var question = Titanium.UI.createLabel({ top : 300, left : 150, width : 'auto', height : 'auto', text : 'Question is here.' }); win1.add(question); openButton.addEventListener('click', function(e) { Ti.API.info("questionPresent " +questionPresent); if (questionPresent == 0) {//if no question is present, create the question label question.show(); //adds the question to the view questionPresent = questionPresent = 1; //updates the "questionPresent" value, meaning question is now present. } else if (questionPresent == 1) {//if question is present, remove it. this is where the error occurs question.hide(); } });
Your Answer
Think you can help? Login to answer this question!