Windows - The best way to use them

You must Login before you can answer or comment on any questions.

Im writing an iPad application at present for my son. On load the app.js has a simple menu which has some basic buttons on it which load seperate windows which all works cool.

Inside the child window I have a button to close this window which returns back to the menu screen (app.js). If I repeat this operation two more times the close button in the child screen then does not work anymore.

http://www.youtube.com/watch?v=7gfge7T7Xrg

Has anyone else had this kind of issue before - Is there a better solution.

app.js example

/* ------------- [ Master Window - Menu ] ----------------- */
var master = Titanium.UI.createWindow({  
    backgroundColor:'#39f',
    tabBarHidden:true
});
 
/* ------------- [ Menu UI / Event Listeners ] ----------------- */
var button = Ti.UI.createButton({width:100, height:50, title:'Windmills', top:10, left:10})
 
master.add(button);
 
button.addEventListener('click', function(e)
{
    windmillsWin.open()
})
 
master.open()
windmill.js code
var w = Ti.currentWindow
 
var button = Ti.UI.createButton({width:100, height:50, zIndex:100, title:'Close', top:10, left:10})
button.addEventListener('click', function(e)
{
    w.close()
});
w.add(button)

1 Answer

Steve

You really need to use a js validator - try Titanium Studio, which will help you to avoid simple errors.

Hence, I have improved your code, and included inline comments:

app.js:

var master = Titanium.UI.createWindow({
  backgroundColor:'#39f',
  tabBarHidden:true,
  title:'master Window'
});
 
var button = Ti.UI.createButton({
  width:150,
  height:50,
  title:'Windmills',
  top:10,
  left:10
});
 
master.add(button);
 
master.open();
 
// placing eventListeners after window open will improve app responsiveness
button.addEventListener('click', function(e){
  // window should be created inside the event,
  // so that it is properly destroyed when closed
  var windmillsWin = Titanium.UI.createWindow({
    backgroundColor:'green',
    // are you sure there is a need to create a new context?...
    url:'windmill.js',
    title:'windmillsWin Window'
  });
 
  windmillsWin.open();
})
// it's Ti.UI...!!!
var w = Ti.UI.currentWindow;
 
var button = Ti.UI.createButton({
  width:100,
  height:50,
  zIndex:100,
  title:'Close',
  top:10,
  left:10
});
 
w.add(button);
 
button.addEventListener('click', function(e){
  w.close();
});
Hope this helps

— answered 2 years ago by Paul Dowsett
answer permalink
2 Comments
  • I must be winding you boys up this evening - Thanks again!

    — commented 2 years ago by Steve Clark

  • Hey, there is no need to think like that, Steve! Your question is considered a contribution, as it may well help someone else in future. Good luck with your app - be sure to ask again if you need to. :)

    — commented 2 years ago by Paul Dowsett

Your Answer

Think you can help? Login to answer this question!