new screen to fire when nav bar button pushed

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

how would you have a new window show or pop open when you hit a nav bar button at the top? As of right now, when I fire the application i want the main screen to show my selections for the app. then from there if a button at the top in the nav bar is pressed, i would like it to pull up a new screen of a list of books. When i hit the button as of right now, i don't get a new screen at all, i get the screen from the app.js. not like i want anything on the screen to work, i just would like a new screen to show instead of the screen from the app.js file.

Here is my app.js file.

var win = Titanium.UI.createWindow({
 //   backgroundColor : 'white'
});
 
var tb1 = Titanium.UI.createTabbedBar({
    labels : ['Sports', 'Map', 'Books', 'Contact'],
    backgroundColor : '#336699',
    top : 0,
    style : Titanium.UI.iPhone.SystemButtonStyle.BAR,
    height : 40,
    width : '100%',
    index : 1,
});
win.add(tb1);
 
var newWin = Ti.UI.createWindow({
    url : 'book.js'
});
 
tb1.addEventListener('click', function(e) {
    if(e.index == 0) {
        var newWin = Ti.UI.createWindow({
            url : 'book.js',
            top : 41
        });
        newWin.open();
    }
    if(e.index == 1) {
        var newWin = Ti.UI.createWindow({
            url : 'info.js',
            top : 41
        });
        newWin.open();
    }
    if(e.index == 2) {
        var newWin = Ti.UI.createWindow({
            url : 'map.js',
            top : 41
        });
        newWin.open();
    }
    if(e.index == 3) {
        var newWin = Ti.UI.createWindow({
            url : 'galary.js',
            top : 41
        });
        newWin.open();
    }
})
 
win.open();
 
 
//var win = Titanium.UI.createWindow({
//  title:"title", 
//  backgroundColor:"#FFFFFF",
 
 
//})
 
//label
var label = Titanium.UI.createLabel({
 
    backgroundImage: "images/image.png",
    top: "10%",
    width: "100%",
    height: "12%",
    textalign: "center",
})
 
// new button
 
var openWebpagenotification = Titanium.UI.createButton({
    title:"Notifications",
    backgroundImage: "images/notificationbutton.png",
    width:"100%",   
    height:"12%",
    top:"20%",
    id: "Notifications",
    url:"http://www.google/",           color: '#000000'                                                    
 
});
 
 
 
openWebpagenotification.addEventListener("click", function(e){
 
 
// Create a window and add a webview to it that opens your url:
var webviewWindow = Titanium.UI.createWindow({title: 'Notifications', fullscreen: false});           
var myWebView = Ti.UI.createWebView({url:e.source.url}); 
webviewWindow.add(myWebView); 
 
// Will need a button to close the new window:
var closeButton = Titanium.UI.createButton({title:'Close', style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN});
webviewWindow.setLeftNavButton(closeButton);
 
closeButton.addEventListener('click',function(evt){
    webviewWindow.close();
});
 
webviewWindow.open({modal:true});
webviewWindow.open();  // Open your new window 
})
 
 
 
 
win.add(label);
win.add(openWebpagenotification); 
win.open();'

now here is the book.js file - this is where i would like a plain screen to show a listing of books. BOOK.js

//////////

function tibook() {
    var BookWindow = require('book');
    var BookWindow = new BookWindow();
    bookWindow.open();
 
 
var tb1 = Titanium.UI.createTabbedBar({
    labels : ['Sports', 'Map', 'Books', 'Contact'],
    backgroundColor : '#336699',
    top : 0,
    style : Titanium.UI.iPhone.SystemButtonStyle.BAR,
    height : 40,
    width : '100%',
    index : 1,
});
win.add(tb1);
 
var newWin = Ti.UI.createWindow({
    url : 'book.js'
});
 
tb1.addEventListener('click', function(e) {
    if(e.index == 0) {
        var newWin = Ti.UI.createWindow({
            url : 'book.js',
            top : 41
        });
        newWin.open();
    }
    if(e.index == 1) {
        var newWin = Ti.UI.createWindow({
            url : 'geo.js',
            top : 41
        });
        newWin.open();
    }
    if(e.index == 2) {
        var newWin = Ti.UI.createWindow({
            url : 'map.js',
            top : 41
        });
        newWin.open();
    }
    if(e.index == 3) {
        var newWin = Ti.UI.createWindow({
            url : 'galary.js',
            top : 41
        });
        newWin.open();
    }
})
 
win.open();
 
 
 
 
    return self;
};
////////

Thanks for your thoughts and expertise. -paul

1 Answer

You're using a mix of multiple context and commonjs coding here that is just bound to get your wires crossed. When you open a window with a url property, you are creating that window inside a new context. Then inside that new context, it looks like you are suddenly trying to format your code commonjs style. Instead, in your app.js file, you should just require the book as you do in the second file and make that your newWin. Then you can open newWin inside the current context and maybe even reuse the tabbedBar instead of writing all that code again. Code like that should remain DRY (dont repeat yourself).

— answered 9 months ago by Anthony Decena
answer permalink
3 Comments
  • I think i have an idea with what you are saying... so i left my app.js the same and make this as my book.js file and took out the rest.

    var bookWindow = require('book');
        var bookWindow = new bookWindow();
        bookWindow.open();
    And that was the only code that I have in there now at all in the book.js. I get this error.

    Test[22613:1b803] *** Terminating app due to uncaught exception of class '__NSCFDictionary' terminate called throwing an exception

    Any thoughts would be greatly appreciated. Thanks! - paul

    — commented 9 months ago by paul wamser

  • I even tried it like this as well....

    function tibook() {
        var bookWindow = require('book');
        var bookWindow = new bookWindow();
        bookWindow.open();
     
     
            return win;
    };
    Now i don't get an eror with this, but i still see the main screen from the app.js file. I would like to be able to pull up just a list of books that i can input there myself. thanks for your thoughts. regards, -paul

    — commented 9 months ago by paul wamser

  • You're adding an entire level of complexity that isn't necessary. Instead of doing all that using a url property of a window, your app.js should just look like this:

    tb1.addEventListener('click', function(e) {
        if (e.index == 0) {
            var BookWin = require('book');
            var newWin = new BookWin();
            newWin.open();
        }
        // etc.
    });

    — commented 9 months ago by Anthony Decena

Your Answer

Think you can help? Login to answer this question!