首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >钛-将变量传递到新窗口

钛-将变量传递到新窗口
EN

Stack Overflow用户
提问于 2012-04-11 00:46:57
回答 1查看 7.2K关注 0票数 0

我在我的iphone钛应用程序中传递变量时遇到了问题。下面是我的AppTabGroup.js文件。有一个事件侦听器名为“赠款参与”。当触发这个选项卡时,我需要将变量event.name和event.email传递给所有新的选项卡。因为某种原因,这对我不起作用。最后我得到了未定义的错误。我想我的问题是如何设置全局变量?就像event.name一样,所以它在应用程序中的任何地方都会出现。

代码语言:javascript
运行
复制
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选项卡

代码语言:javascript
运行
复制
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;
EN

回答 1

Stack Overflow用户

发布于 2012-04-11 11:05:28

app.js文件中定义的变量应该可以在所有其他JS文件中使用。但是,您应该为这些全局变量创建一个命名空间,以确保它们不会干扰任何其他变量。

下面是一个例子,说明你如何做到这一点。在app.js中:

代码语言:javascript
运行
复制
var MyGlobalVars = {
    email: null,
    name: null
};

然后,通过引用以下内容,可以从应用程序的任何位置获取和设置这些值:

代码语言:javascript
运行
复制
MyGlobalVars.email = 'me@gmail.com';
alert('My email is: '+MyGlobalVars.email);

然而,这些值不会存储在您的应用程序的执行之间。如果您停止应用程序,并再次启动它,这些变量将丢失,直到您再次设置它们。如果您希望存储这些信息,以便在应用程序重新启动后能够再次访问这些信息,那么可以使用Ti.App.Properties来存储它们。

这里有关于存储信息的不同方式的信息:

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10098442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档