javascriptiosiphonetitaniumtitanium-mobile

Using Titanium, Remove current view or pop current view


I have to remove current view being in the same view... if i am in the parent view i can do

parentView.remove(childView);

but being on the child view i am not having parentView so how can i pop childView to get parentView on top, as it happens on pressing the back button in iOS?? please help

Here is my childView file..

function DetailView(){


var self = Ti.UI.createView({
    backgroundColor:'#fff'
});
// Create a Button.
var aButton = Ti.UI.createButton({
    title : 'aButton',
    height : '50',
    width : '100',
    top : '10',
    left : '20'
});

// Listen for click events.
aButton.addEventListener('click', function() {

     alert('\'aButton\' was clicked!');

I have to navigate back on press of aButton, what should i Put here to do that

});

// Add to the parent view.
self.add(aButton);


return(self);
 }
module.exports = DetailView;

Here is my parent view:

    //FirstView Component Constructor
    var self = Ti.UI.createView();

    function FirstView() {
//create object instance, a parasitic subclass of Observable


var DetailView = require('ui/common/DetailView');

var data = [{title:"Row 1"},{title:"Row 2"}];
var table = Titanium.UI.createTableView({
    data:data
    });
table.addEventListener('click', rowSelected);

self.add(table);

return self;
    }

    function rowSelected()
    {
var DetailView = require('ui/common/DetailView');

//construct UI
var detailView = new DetailView();
self.add(detailView);

    }

    module.exports = FirstView;

Solution

  • You can pass your parentView to the constructor of child view at this point:

    //construct UI
    var detailView = new DetailView(parentView);
    self.add(detailView);
    

    and in click event

    aButton.addEventListener('click', function() {
       if ( parentView != null ) {
           parentView.remove(childView);
        }
    });