【问题标题】:Titanium Close Window and Open New OneTitanium 关闭窗口并打开新窗口
【发布时间】:2016-03-17 06:13:16
【问题描述】:

我正在 Titanium 中构建一个 iOS 应用程序。第一个窗口是登录页面。当用户输入他们的用户名和密码时,这些值将被发送到 PHP 文件进行身份验证。

如果用户通过身份验证 - 即他们有唯一的用户名/密码组合,我希望关闭当前窗口并打开新窗口。

值被发送到 PHP 文件并且用户正在被认证;但是,当关闭当前窗口的代码运行 (Titanium.UI.currentWindow.close) 时,会引发错误,指示打开新窗口的文件不存在。不过,引用新窗口的文件确实存在。

我已将似乎导致错误的代码移到许多地方,但结果相同。

var loginReq = Titanium.Network.createHTTPClient();

loginReq.onload = function()
{
    var json = this.responseText;
    var response = JSON.parse(json);
    if (response.logged == true)
    {
        alert("Welcome " + response.name + ". Your email is: " + response.email);
        username.value = '';
        password.value = '';
        //This creates the new window after user authentication.
        var menuPage = Titanium.UI.createWindow({
        title: "Menu",
        tabBarHidden: false,
        url: 'menuPage.js'
        });
        //This is supposed to close the current window.
        Titanium.UI.currentWindow.close();
        //This is supposed to open the new window.
        menuPage.open();
    }
    else
    {
        alert(response.message);
    }
};

【问题讨论】:

  • 有时Titanium.UI.currentWindow 不会返回当前窗口。尝试在您的网络文件中传递Window 或在您的窗口中写入网络的onload 作为回调。

标签: titanium


【解决方案1】:

与其先关闭窗口,再打开新窗口,不如先打开新窗口,打开后,从新窗口中关闭旧窗口。

menuPage.open({closeWindow: function(){ 
    curWin.close(); }
});

然后,在menuPage.js 中,注意open 事件,一旦触发,调用您传递给menuPage.js 的函数。

不过,我建议您深入研究 Alloy。这种方法非常过时了。

在合金中你会这样做:

Alloy.createController('menuPage', {closeWindow: function(){ 
    $.getView().close();
});

在菜单页面中:

$.getView().addEventListener('open',args.closeWindow());

【讨论】:

    【解决方案2】:

    你好,试试这个打开一个窗口

    var menuPage = require('menuPage');
    win = new menuPage({title:''});
    

    谢谢

    【讨论】:

      【解决方案3】:

      如果您使用合金:

      文件windowlogin.xml

      <Alloy>
      	<Window id="window_login">
      		<TextField id="txt_username"></TextField>
      		<TextField id="txt_password"></TextField>
      		<Button title="Login" id="btn_login"></Button>
      	</Window>
      </Alloy>

      文件windowlogin.js

      $.btn_login.addEventListener("click", function(e){
      	var loginUrl = "http://domain.com/login";
      	var dataLogin = {
      		username: $.txt_username.value,
      		password: $.txt_password.value
      	};
      	var loginReq = Titanium.Network.createHTTPClient();
      
      	loginReq.onload = function()
      	{
      	    var json = this.responseText;
      	    var response = JSON.parse(json);
      	    if (response.logged == true)
      	    {
      	        alert("Welcome " + response.name + ". Your email is: " + response.email);
      	        username.value = '';
      	        password.value = '';
      	        //This creates the new window after user authentication.
      	        var menuPage = Titanium.UI.createWindow({
      			title: "Menu",
      			tabBarHidden: false,
      			url: 'menuPage.js'
      			});
      			//This is supposed to open the new window.
      	        menuPage.open();
      	        //This is supposed to close the current window.
      	        $.window_login.close();
      	    }
      	    else
      	    {
      	        alert(response.message);
      	    }
      	};
      	
      	loginReq.open("POST", loginUrl);
      	loginReq.send(dataLogin);
      });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-17
        • 2018-05-16
        • 1970-01-01
        • 1970-01-01
        • 2014-07-20
        • 1970-01-01
        • 2014-08-29
        • 1970-01-01
        相关资源
        最近更新 更多