delete an imageVew after adding it

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

I have created and added an imageView in to a View and i want to remove that imageView on a different button press.

My current code is:

var self = Ti.UI.createWindow({
        backgroundColor:'white',
        layout: 'vertical'
    });
 
    var pin = [];
 
 
    var press = Ti.UI.createWindow({
        backgroundColor:'red',
        height: 100,
        layout: 'horizontal'
    });
 
    self.add(press);
    var butView = Ti.UI.createView({
        backgroundColor:'white',
        layout: 'horizontal'
    });
 
    var but1 = Ti.UI.createButton({
        height: 90,
        width: "33%",
        title: "1"
 
    });
    but1.addEventListener('click',function(e){
        pin.push(1);
        if(pin.length > 3){
            alert(pin[0]+","+pin[1]+","+pin[2]+","+pin[3]);
        }
        var dot = Ti.UI.createImageView({
        image: '/images/dot.png',
        width: "25%"
 
    });
        press.add(dot);
    });
    butView.add(but1);
 
    var backBut = Ti.UI.createButton({
        height: 90,
        width: "33%",
        title: "Back"
 
    });
    backBut.addEventListener('click',function(e){
        pin.pop();
         // remove the last added dot imageView added.
        alert(press.getChildren);
        press.remove(dot);
    });
    butView.add(backBut);
 
    self.add(butView);
    self.open();
I have tried to remove it using :
view.remove(imageView);
But this does not work I assume it is because it is not in the current scope.

Is there a way i can say what was the last object added and remove it?

thanks

1 Answer

Use something like this.... you got to save the elements so you can remove them

var self = Ti.UI.createWindow({
        backgroundColor:'white',
        layout: 'vertical'
    });
 
    var pin = [];
    var dotArray=[];
 
    var press = Ti.UI.createWindow({
        backgroundColor:'red',
        height: 100,
        layout: 'horizontal'
    });
 
    self.add(press);
    var butView = Ti.UI.createView({
        backgroundColor:'white',
        layout: 'horizontal'
    });
 
    var but1 = Ti.UI.createButton({
        height: 90,
        width: "33%",
        title: "1"
 
    });
    but1.addEventListener('click',function(e){
        pin.push(1);
        if(pin.length > 3){
            alert(pin[0]+","+pin[1]+","+pin[2]+","+pin[3]);
        }
 
        Ti.API.info(index);
        dotArray.push(Ti.UI.createImageView({
        image: '/images/dot.png',
        width: "25%"
 
    }));
        var index=dotArray.length-1;
        press.add(dotArray[index]);
 
    });
    butView.add(but1);
 
    var backBut = Ti.UI.createButton({
        height: 90,
        width: "33%",
        title: "Back"
 
    });
    backBut.addEventListener('click',function(e){
        pin.pop();
         // remove the last added dot imageView added.
        //alert(press.getChildren);
 
        press.remove(dotArray.pop());
    });
    butView.add(backBut);
 
    self.add(butView);
    self.open();

Your Answer

Think you can help? Login to answer this question!