2015-07-19 135 views
1

我正在钛中构建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); 
    } 
}; 
+0

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

回答

0

嗨试试这个打开的窗口

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

感谢

2

而不是首先关闭窗口,然后打开新的,你应该先打开新的窗口,一旦打开,关闭旧窗口。

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

然后,在menuPage.js,观看了该open事件,一旦被触发,叫你传递给menuPage.js功能。

然而,我会建议潜入合金。这种方法是非常过时。如果您在使用合金

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

在合金你会做这样的:

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

在menupage

文件windowlogin.xml

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

文件windowlogin.js

$.btn_login.addEventListener("click", function(e){ 
 
\t var loginUrl = "http://domain.com/login"; 
 
\t var dataLogin = { 
 
\t \t username: $.txt_username.value, 
 
\t \t password: $.txt_password.value 
 
\t }; 
 
\t var loginReq = Titanium.Network.createHTTPClient(); 
 

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