Search code examples
titaniumtitanium-mobile

Titanium Mobile 3.0 Navigation Controller pass variables between windows


I am using swanify's Titanium Navigation Controller https://github.com/swanify/Titanium-Navigation-Controller for my app deployed in Android device.

i am trying to pass data that i have selected from a table row here back to the previous page and populate a text field there.

Anyone has any ideas how to do it? i am looking into using events but it doesnt seem to be working for me.

thank you. :)


Solution

  • Here is what I have done in this situation, It makes use of the eventing aspects of JavaScript and Titanium, the general principle is to have your own custom events, fire them on a control, and listen elsewhere.

    First lets say we have a file named NextWindow.js that is a CommonJS module encapsulating a window and a tableview, everytime the a row is clicked we capture that event, then fire our own custom event on the window:

    function NextWindow() {
        var self = Ti.UI.createWindow();
        var tableView = Ti.UI.createTableView();
        // Other initialization
        ....
        ....
        tableView.addEventListener('click', function(e) {
            // You may have to use e.rowData.title depending on how you created the table view
            var rowTitle = e.row.title;
            // Now fire a custom event on the window whenever a row is selected
            // send the title through as data
            self.fireEvent('table_row_selected', {title : rowTitle});
    
        });
    
        return self;
    }
    module.exports = NextWindow;
    

    When you create a NextWindow to push onto the navigation stack, add a listener for a custom event to it, this is inside of the previous window with the text box:

    var NextWindow = require('NextWindow');
    var nextWindow = new NextWindow();
    nextWindow.addEventListener('table_row_selected', function(e) {
        // Do what you want here with the passed back data 
        var title = e.title;
        some_label.text = title;
    });
    // Open the next window on the NavigationController stack
    nav.open(nextWindow);
    

    Now were listening for a custom event attached to the nextWindow. Say you have a TableView in your nextWindow, listen for the TableView click, and fire the custom event: