【问题标题】:Titanium - Passing Variable to new windowTitanium - 将变量传递到新窗口
【发布时间】:2012-04-11 00:46:57
【问题描述】:

我在我的 iphone Titan 应用程序中传递变量时遇到问题。下面是我的 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 选项卡

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;

【问题讨论】:

  • 错误是什么,event.name 或 event.email 是什么?活动如何知道电子邮件、姓名?

标签: titanium appcelerator titanium-mobile appcelerator-mobile


【解决方案1】:

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

下面是一个示例,说明您可以如何做到这一点。在 app.js 中:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多