0

我在钛手机中创建了这个简单的登录应用程序。这是我的代码:

登录.js

function Login() {
    var loginView = Titanium.UI.createView({
        backgroundColor:'#C4FBFF',
        layout:'vertical'
    });

    var txtUsername = Titanium.UI.createTextField({
        width:'75%',
        hintText:'Username',
        borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
    });

    var txtPassword = Titanium.UI.createTextField({
        width:'75%',
        hintText:'Password',
        borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED,
        passwordMask:true
    });

    var btnLogin = Titanium.UI.createButton({
        title:'Login',
        width:'75%'
    });

    loginView.add(txtUsername);
    loginView.add(txtPassword);
    loginView.add(btnLogin);

    btnLogin.addEventListener('click',function(e){
        var alertDialog = Titanium.UI.createAlertDialog({ 
            title: 'Confirmation', 
            message: 'You will be logged in as ' + txtUsername.value + ', continue?',
            buttonNames: ['Yes','No'] 
        });

        alertDialog.addEventListener('click',function(e){
            if (e.index === 0){ //Yes Pressed
                var MainMenu = require('ui/common/MainMenu');

                var mainMenuWindow = Titanium.UI.createWindow({
                    backgroundColor:'#336699',
                    title:'Main Menu',
                    modal:true
                });

                var mainMenu = new MainMenu();
                mainMenuWindow.add(mainMenu);

                mainMenuWindow.open();
            }
            else{ // No Pressed
                makeAlert('Login','Please contact your system administrator');
            }
        });

        alertDialog.show();
    });

    function makeAlert(title, message) {
        var customAlertDialog = Titanium.UI.createAlertDialog({ 
            title: title, 
            message: message,
            buttonNames: ['Ok']
        });
        return customAlertDialog.show();
    }

    return loginView;
}

module.exports = Login;

这是我的 MainMenu.js :

function MainMenu(){
    var mainMenuView = Titanium.UI.createView({
        backgroundColor:'#C4FBFF',
        layout:'vertical'
    });

    var btnClose = Titanium.UI.createButton({
        title:'Close',
        height:30,
        width:150
    });

    mainMenuView.add(btnClose);

    btnClose.addEventListener('click',function(e){
    //What should i do here?
    }); 
    return mainMenuView;
}    
module.exports = MainMenu;

主要问题是我想在按下关闭按钮时关闭模式窗口。我已经尝试过Titanium.UI.currentWindow.close(),但它关闭了我所有的应用程序,并且无法在 iOS 设备上执行。有什么建议吗??非常感谢..

4

1 回答 1

3
// pass parent window in as parameter
var mainMenu = new MainMenu(mainMenuWindow);
mainMenuWindow.add(mainMenu);

现在使用参数

function MainMenu(_parent /*parent window*/){

    // other code ...

    btnClose.addEventListener('click',function(e){
        _parent.close();
    });

}
于 2012-10-12T12:02:48.217 回答