0

我在我的iphone鈦應用程序中傳遞變量時出現問題。以下是我的AppTabGroup.js文件。有一個名爲「grantEntrance」的事件監聽器。當這個被觸發時,我需要將變量event.name和event.email傳遞給所有新的選項卡。由於某種原因,這不適合我。我最終得到未定義的錯誤。我想我的問題是如何設置全局變量?像event.name一樣,它顯示在應用程序的任何地方。鈦 - 傳遞變量到新窗口

function AppTabGroup() { 
//declare module dependencies 
var AppWindow = require('ui/AppWindow'); 
var OverviewWindow = require('ui/OverviewWindow'); 
var LeadWindow = require('ui/LeadWindow'); 
var CaseWindow = require('ui/CaseWindow'); 
var ResourcesWindow = require('ui/ResourcesWindow'); 

//create module instance 
var self = Ti.UI.createTabGroup(); 

//create app tabs 
var win1 = new OverviewWindow(L('Login')), 
    win2 = new CaseWindow(L('Cases')); 
    win3 = new LeadWindow(L('Leads')); 
    win4 = new ResourcesWindow(L('Resources')); 

var tab1 = Ti.UI.createTab({ 
    title: L('Login'), 
    icon: '/images/KS_nav_ui.png', 
    window: win1 
}); 
win1.containingTab = tab1; 

var tab2 = Ti.UI.createTab({ 
    title: L('Cases'), 
    icon: '/images/KS_nav_views.png', 
    window: win2 
}); 
win2.containingTab = tab2; 

var tab3 = Ti.UI.createTab({ 
    title: L('Leads'), 
    icon: '/images/KS_nav_views.png', 
    window: win3 
}); 
win3.containingTab = tab3; 

var tab4 = Ti.UI.createTab({ 
    title: L('Resources'), 
    icon: '/images/KS_nav_mashup.png', 
    window: win4 
}); 
win4.containingTab = tab4; 

//Load Initial Login tab 
self.addTab(tab1); 

//If Login is successful then the below even will fire and the other tabs will be loaded 
Ti.App.addEventListener('grantEntrance', function(event) 
{ 
win2.name  = event.name; 
win2.email  = event.email; 
self.addTab(tab2); 
self.addTab(tab3); 
self.addTab(tab4); 
self.removeTab(tab1); 

}); 



return self; 
}; 

module.exports = AppTabGroup; 

下面是我CaseWindow.js標籤在app.js文件中定義應該是在所有其他的JS文件可用

function AppWindow(title) { 
var Main = Ti.UI.createWindow({ 
    title:title, 
    backgroundColor:'white' 
}); 

//Closed Case Button 
var ccButton = Titanium.UI.createButtonBar({ 
    labels:['Closed'], 
    backgroundColor:'#336699' 
}); 
Main.setLeftNavButton(ccButton); 

//New Case Button 
var ncButton = Titanium.UI.createButtonBar({ 
    labels:['New Case'], 
    backgroundColor:'#336699' 
}); 
Main.setRightNavButton(ncButton); 

ncButton.addEventListener('click', function() { 
    //containingTab attribute must be set by parent tab group on 
    //the window for this work 
    Main.containingTab.open(Ti.UI.createWindow({ 
     title: L('newCaseWindow'), 
     backgroundColor: 'white' 
    })); 
}); 

var msg = Titanium.UI.createLabel({ 
text:"Your Name is " + win.name, 
//text:"You have successfully logged in. Upon logging in we sent back your email address and your name. You can pass all kinds of data simply by creating objects on your window.\n\nYour email is:\n" + email + "\n\nyour name is:\n" + name, 
top:10, 
left:10, 
width:300, 
height:'auto' 
}); 
    Main.add(msg); 

    return Main; 
}; 

module.exports = AppWindow; 
+0

什麼是錯誤的,什麼是event.name或event.email?該活動如何瞭解電子郵件,姓名? – 2012-04-11 05:41:09

回答

2

變量。您應該爲這些全局變量創建一個名稱空間,以確保它們不會干擾任何其他變量。

下面是一個如何做到這一點的例子。在app.js:

var MyGlobalVars = { 
    email: null, 
    name: null 
}; 

然後你就可以獲取和設置從應用程序中的任何地方,這些價值通過引用:

MyGlobalVars.email = '[email protected]'; 
alert('My email is: '+MyGlobalVars.email); 

但是,這些值不會被存儲在執行之間你的應用程序。如果你停止你的應用程序,並重新啓動它,這些變量將會丟失,直到你再次設置它們。如果您希望存儲信息以便在應用程序重新啓動後再次訪問它們,可以使用Ti.App.Properties來存儲它們。

上有不同的方式在這裏保存的信息:

https://wiki.appcelerator.org/display/guides/Working+with+Local+Data+Sources