0
votes

I'm trying to add a right nav button in iOS to a windows in Titanium.

The code is just the original "master/detail" starting template provided by Titanium and then in MasterView.js, I try to add a button to the navbar.

But it doesn't work like it was supposed to:

var addBtn = Ti.UI.createButton({
    systemButton:Ti.UI.iPhone.SystemButton.ADD
});
//self.setRightNavButton(addBtn);
var win1 = Titanium.UI.currentWindow;
win1.setRightNavButton(addBtn);

This fails with the error:

[ERROR] : Script Error = 'undefined' is not an object (evaluating 'win1.setRightNavButton') at MasterView.js (line 14).

How can I add this button to the navbar then? I've seen some examples but they all rely in having the navbar declared in place. In this case that ain't possible since Titanium declared the navbar in the AplicationWindow.js specific to each platform and then calls the MasterView.js function and file where I'm supposed to define the navbar button.

2
It's better to keep a reference to the window than using currentWindow. Till today i haven't figured out how & when to use it and if it works at all. - mr.VVoo
Well, I ended up just creating a simple project and calling each window by a function pointing to it's javascript file name. Seems a bit archaic, but it works now. - jbssm

2 Answers

0
votes

Ti.UI.currentWindow only works if there is a window already open. The error you have is coming from the fact that when you create the MasterView, you have not yet opened up the window.

Look inside ApplicationWindow.js, you will see that you create the masterview before even opening the window.

If you want to set navbar items, either add them in the ApplicationWindow, app.js, or pass the window to the MasterView.

0
votes

Ti.UI.currentWindow only works if a window is opened using the url parameter. In that case, the controller that is referenced by the url parameter will have the Ti.UI.currentWindow property set to the window.