1

am having troubles on my titanium mobile project. The code below displays a login page which when the login button is clicked, it will re-direct user to the tabGroup.(tab1 and tab2).

// this sets the background color of the master
// UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#fff');

// create tab group
var tabGroup;

var win = Titanium.UI.createWindow({
    title:'TabViewLogin',
    tabBarHidden:true,
});


var username = Titanium.UI.createTextField({
    color:'#336699',
    top:10,
    left:10,
    width:300,
    height:40,
    hintText:'Username',
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win.add(username);

var password = Titanium.UI.createTextField({
    color:'#336699',
    top:60,
    left:10,
    width:300,
    height:40,
    hintText:'Password',
    passwordMask:true,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win.add(password);

var loginBtn = Titanium.UI.createButton({
    title:'Login',
    top:110,
    width:90,
    height:35,
    borderRadius:1,
    font:{fontWeight:'bold',fontSize:14}
});
win.add(loginBtn);
win.open();

Titanium.App.addEventListener('logout', function(e) {
    win.open();
    tabGroup.close();
});

loginBtn.addEventListener('click',function(e)
{
    if ( tabGroup == undefined ) {
        tabGroup = Titanium.UI.createTabGroup();
        //
        // create base UI tab and root window
        //
        var win1 = Titanium.UI.createWindow({  
            title:'Tab 1',
            backgroundColor:'#fff'
        });
        var tab1 = Titanium.UI.createTab({  
            icon:'KS_nav_views.png',
            title:'Tab 1',
            window:win1
        });

        var label1 = Titanium.UI.createLabel({
            color:'#999',
            text:'I am Window 1',
            font:{fontSize:20,fontFamily:'Helvetica Neue'},
            textAlign:'center',
            width:'auto'
        });

        win1.add(label1);

        //
        // create controls tab and root window
        //
        var win2 = Titanium.UI.createWindow({  
            title:'Tab 2',
            backgroundColor:'#eee'
        });
        var tab2 = Titanium.UI.createTab({  
            icon:'KS_nav_ui.png',
            title:'Tab 2',
            window:win2
        });

        var label2 = Titanium.UI.createLabel({
            color:'#999',
            text:'I am Window 2',
            font:{fontSize:20,fontFamily:'Helvetica Neue'},
            textAlign:'center',
            width:'auto'
        });

        win2.add(label2);

        var button = Ti.UI.createButton({
            title: "Logout",
            style: Ti.UI.iPhone.SystemButtonStyle.DONE

        });

        button.addEventListener('click',function(e)
        {
            Ti.App.fireEvent('logout');
        });

        win1.setRightNavButton(button);     
        win2.setRightNavButton(button);     

        //
        //  add tabs
        //
        tabGroup.addTab(tab1);  
        tabGroup.addTab(tab2);  

    } 

    // open tab group
    win.close();
    tabGroup.open();

});

BUT the problem now is, i need it to authenticate users first with the code below before re-directly them to the tabGroup.

Cloud.Users.create({
    email: 'test@mycompany.com',
    first_name: 'test_firstname',
    last_name: 'test_lastname',
    password: 'test_password',
    password_confirmation: 'test_password'
}, function (e) {
    if (e.success) {
        var user = e.users[0];
        alert('Success:\n' +
            'id: ' + user.id + '\n' +
            'sessionId: ' + Cloud.sessionId + '\n' +
            'first name: ' + user.first_name + '\n' +
            'last name: ' + user.last_name);
    } else {
        alert('Error:\n' +
            ((e.error && e.message) || JSON.stringify(e)));
    }
});

THANKS FOR ANY ASSISTANCE

4

1 回答 1

0

用于Ti.App.Properties.setBoolean(key, value)存储用户是否已登录并Ti.App.Properties.getBoolean(key, defaultValue)检查用户是否已登录。

要从代码中切换选项卡,您可以使用:Titanium.UI.TabGroup.setActiveTab()方法。

还可以尝试使用 Alloy 代码,该代码负责从处理事件的代码创建视图并更改应用程序(控制器)的状态。

于 2013-10-06T00:58:57.177 回答