2013-03-02 37 views

回答

0

這就是我在這種情況下所做的,它利用JavaScript和Titanium的事件方面,一般原則是擁有自己的自定義事件,在控件上觸發它們並在別處聽取。

首先,假設我們有一個名爲NextWindow.js文件,該文件是一個CommonJS的模塊封裝窗口和實現代碼如下,每次在單擊某一行,我們捕獲的事件,然後觸發我們自己的自定義事件窗口上:

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; 

當你創建一個NextWindow推到導航堆棧,添加一個監聽器自定義事件給它,這是一個窗口與文本框的裏面:

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); 

現在正在傾聽附加到nextWindow的自定義事件。假設您在您的nextWindow中有一個TableView,聽取TableView點擊並激發自定義事件:

+0

像魔術一樣工作。先生非常感謝您。我做的唯一另外的事情是在事件發生後執行'self.close()'。 :) – chongzixin 2013-03-03 10:25:43