Trouble with EventListener in Titanium Studio - titanium

I am having trouble trying to add an Event Listener to my button. Here is what I have in my code.
var TabGroup = Titanium.UI.createTabGroup();
var Maps = Titanium.UI.createWindow({
backgroundImage:'/images/Background2.jpg'
});
var tab1 = Titanium.UI.createTab({
title: 'Maps',
icon: '/KS_nav_ui.png',
window: Maps
});
var scrollView = Titanium.UI.createScrollView({
contentWidth:'auto',
contentHeight:'auto',
top:0,
showVerticalScrollIndicator:false,
showHorizontalScrollIndicator:false
});
var view = Ti.UI.createView({
height:495,
width:300
});
var btnInnovations = Ti.UI.createButton({
height:75,
width:Titanium.UI.FILL,
backgroundImage:'/images/Innovations.jpg',
top:60
});
var btnOaks = Ti.UI.createButton({
height:75,
width:Titanium.UI.FILL,
backgroundImage:'/images/Oaks Campus.jpg',
top:145
});
var btnWHQ = Ti.UI.createButton({
height:75,
width:Titanium.UI.FILL,
backgroundImage:'/images/WHQ.jpg',
top:230
});
var btnRiverport = Ti.UI.createButton({
height:75,
width:Titanium.UI.FILL,
backgroundImage:'/images/Riverport.jpg',
top:315
});
var btnContinuous = Ti.UI.createButton({
height:75,
width:Titanium.UI.FILL,
backgroundImage:'/images/Continuous.jpg',
top:400
});
view.add(btnInnovations);
view.add(btnOaks);
view.add(btnWHQ);
view.add(btnRiverport);
view.add(btnContinuous);
scrollView.add(view);
Maps.add(scrollView);
TabGroup.addTab(tab1);
TabGroup.open();
btnInnovations.addEventListener('click', function(e){
var InnovationsFloors = Titanium.UI.createWindow({
title: 'Innovations Floors',
url:'InnovationsFloors.js'
});
InnovationsFloors.open({modal : true, backgroundImage:'images/Background1.jpg'});
The error I get in the emulator says cannot call method open of undefined and if I take out the Titanium.UI.currentTab.open(InnovationsFloors,{animation:true}); it won't even register the click...

First of all you cant have spaces in the name of your Window url field, rename your Innovations Floors.js file to InnovationsFloors.js.
Second part is that the attributes you are passing in to the open() command are not supported, it should be animated not animation, even so, you should not use this in this fashion, I refer you to the docs on this one.
Instead just do this:
Titanium.UI.currentTab.open(InnovationsFloors);
Or try this:
TabGroup.activeTab.open(InnovationsFloors);
If that doesn't work then it means you have not called open on your TabGroup so there is no current tab.
You could also always just try this and open a modal:
InnovationsFloors.open({modal : true});

if you want to open a window with a button try this works for me
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({
title:'Tab 1',
backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({
icon:'KS_nav_views.png',
title:'Tab 1',
window:win1
});
var label1 = Titanium.UI.createLabel({
color:'#999',
text:'I am Window 1',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:'auto'
});
var button = Titanium.UI.createButton({
title: 'Hello',
top: 10,
width: 100,
height: 50
});
win1.add(button)
win1.add(label1);
button.addEventListener('click', function(e){
var win = Titanium.UI.createWindow({
title:'New Window',
backgroundColor:'#fff'
});
win.open();
});
tabGroup.addTab(tab1);
// open tab group
tabGroup.open();

Related

Table view Section Header Title in titanium

I have created a custom Header view for the tableview in my Titanium project,
var headerView = Ti.UI.createView({ height: 40,backgroundColor:'#00928F' });
var headerLabel = Ti.UI.createLabel({ text: array_interview_dates[i],color:'white' });
headerView.add(headerLabel);
headerViewArray.push(headerView);
And now when I want to get the section header Title when I select the row
table.addEventListener('click', function action_table(e){
var header_title=e.section.headerTitle;
}
I get empty value, I want to get the section_HeaderTitle
In your code there is no definition of a section.
You should define the property headerTitle inside the section definition to have it available, like in this example (Adapted from Titanium documentation):
Ti.UI.backgroundColor = 'white';
var win = Ti.UI.createWindow();
var sectionFruit = Ti.UI.createTableViewSection({ headerTitle: 'Fruit' });
sectionFruit.add(Ti.UI.createTableViewRow({ title: 'Apples' }));
sectionFruit.add(Ti.UI.createTableViewRow({ title: 'Bananas' }));
var sectionVeg = Ti.UI.createTableViewSection({ headerTitle: 'Vegetables' });
sectionVeg.add(Ti.UI.createTableViewRow({ title: 'Carrots' }));
sectionVeg.add(Ti.UI.createTableViewRow({ title: 'Potatoes' }));
var table = Ti.UI.createTableView({
data: [sectionFruit, sectionVeg]
});
table.addEventListener('click', function(e){
Ti.API.info(e.rowData.title);
Ti.API.info(e.section.headerTitle);
});
win.add(table);
win.open();

window background image not showing on iphone Titanium

i have created tab group with single tab and window for tab , i have set background image for tab window, image is showing properly on android but in iPhone i cant see any image any solution to this problem?
var tabGroup = Titanium.UI.createTabGroup({
navBarHidden: true,
layout: 'vertical'
});
Ti.UI.Clipboard.setText('');
var db = require('dbhelper');
db.callDb();
var windowTitle = Ti.UI.createLabel({
color: '#fff',
text: 'IEMR LITE'
});
var win1 = Titanium.UI.createWindow({
backgroundColor: '#fff'
});
if (Ti.Platform.name === 'iPhone OS') {
win1.titleControl = windowTitle;
win1.barImage = 'images/actionbar3.png';
win1.hideTabBar();
} else {}
win1.backgroundImage = 'images/default_portrait.png';
I have tried this code and its works for me. also make sure you are providing correct image path.
var tabGroup = Titanium.UI.createTabGroup({
navBarHidden : true,
layout : 'vertical'
});
Ti.UI.Clipboard.setText('');
//var db = require('dbhelper');
//db.callDb();
var windowTitle = Ti.UI.createLabel({
color : '#fff',
text : 'IEMR LITE'
});
var win1 = Titanium.UI.createWindow({
backgroundColor : '#fff'
});
if (Ti.Platform.name === 'iPhone OS') {
win1.titleControl = windowTitle;
win1.barImage = '1.png';
win1.hideTabBar();
} else { }
win1.backgroundImage = '2.png';
var tab1 =Ti.UI.createTab({
window : win1
});
tabGroup.addTab(tab1);
tabGroup.open();
Try deleting your build folder and building for iPhone again. Sometimes after adding an image (or other binary resource), it doesn't get linked to the underlying built project properly until you do a clean build.

How to load another js file on a button click in titanium

I have App.js
(function() {
Window = require('ui/tablet/ApplicationWindow');
}
new Window().open();
})();
From there ApplicationWindow.js is loaded.
In ApplicationWindow.js
function ApplicationWindow() {
//load component dependencies
var FirstView = require('ui/common/FirstView');
//create component instance
var self = Ti.UI.createWindow({
backgroundColor:'#ffffff'
});
var win2 = Titanium.UI.createWindow({
backgroundColor: 'red',
title: 'Red Window'
});
//construct UI
var firstView = new FirstView();
var nav = Titanium.UI.iPhone.createNavigationGroup({
window: win2
});
win2.add(firstView);
self.add(nav);
self.open();
//self.add(firstView);
if (Ti.Platform.osname === 'ipad') {
self.orientationModes = [Ti.UI.LANDSCAPE_LEFT] ;
};
return self;
}
//make constructor function the public component interface
module.exports = ApplicationWindow;
I get a view with 2 textfields and a button login in FirstView.js.The view has a navigation bar with title Red Window. I want to load Home.js on loginButton Click.
Here is the loginButtonClick Code :
loginButton.addEventListener ('click', function(e){
//navigate to Home.js
});
How can I do that.Can anyone please help me out.
Try the following method in your current file
loginButton.addEventListener('click', function(e){
var win = Ti.UI.createWindow({
backgroundColor : 'white',
url : 'home.js' //Path to your js file
});
win.open();
});
home.js
var myWin = Ti.UI.currentWindow;
//You can add your controls here and do your stuff.
// Note that never try to open myWin in this page since you've already opened this window
You can also try the following method
Method 2
//In your current page
loginbutton.addEventListener('click', function(e) {
var Home = require('/ui/common/Home');
var homePage = new Home();
homePage.open();
});
Your Home.js file
function Home() {
var self = Ti.UI.createWindow({
layout : 'vertical',
backgroundColor:'white'
});
//Do your stuff here
//Add other controls here
return self;
}
module.exports = Home;
Look at this answer, it's also same as your question

iOS Back button on centre of window using Titanium

I am trying to place a back button at centre of window. I am able to achieve the same using image view. However navigation to previous windows except for the very last are failing.
Lets say I have traversed three windows A->B->C. From window 'C' the button takes me to window 'B'. But from window 'B' click of same button evokes no response (ideally it should have taken me to window 'A')
The code I have used are as following
{
var imgView = Ti.UI.createImageView({
top:'50%',
left : 0,
image : "icons/back.png"
});
imgView.addEventListener('click', function(){
self.close();
});
}
Add the above line in your each file which containing the new window, since you're opening new windows using url method.
var currentWindow = Ti.UI.currentWindow;
Then close the window as follows
imgView.addEventListener('click', function(){
currentWindow.close();
});
This will close the current window, since currentWindow represents the active window
I'll add the working Example :
app.js
var win = Ti.UI.createWindow({
backgroundColor : 'white'
});
var back = Ti.UI.createButton({
title : 'To win1 ',
width : '70%'
});
back.addEventListener('click', function(){
var win1 = Ti.UI.createWindow({
url : 'win1.js',
backgroundColor : 'white',
layout : 'vertical'
});
win1.open();
});
win.add(back);
win.open();
win1.js
var self = Ti.UI.currentWindow;
var back = Ti.UI.createButton({
title : 'Back to home',
top : 20,
width : '70%'
});
var next = Ti.UI.createButton({
title : 'To win2',
top : 20,
width : '70%'
});
self.add(back);
self.add(next);
back.addEventListener('click',function(){
self.close();
});
next.addEventListener('click', function(){
var win2 = Ti.UI.createWindow({
url : 'win2.js',
backgroundColor : 'white',
layout : 'vertical'
});
win2.open();
});
win2.js
var self = Ti.UI.currentWindow;
var back = Ti.UI.createButton({
title : 'Back to win1',
top : 20,
width : '70%'
});
self.add(back);
back.addEventListener('click',function(){
self.close();
});

Titanium Mobile: cant navigate between pages

i am new to Titanium and i am have 2 seemingly simple problems while trying to use it for the Android.
1) i am trying to navigate to the next page on click of a button. but instead it is showing me a blank black screen. i know my second page CreateNewMeetup.js is correct because i tried displaying it as the landing page of my app and it works. my codes are as follows:-
ApplicationWindow.js
...
var button = Ti.UI.createButton({
height:44,
width:'auto',
title:'Create New Meetup',
top:20
});
self.add(button);
button.addEventListener('click', function() {
var newWindow = Ti.UI.createWindow({
url : "/ui/common/CreateNewMeetupWindow.js",
fullscreen: false
});
newWindow.open();
});
return self;
CreateNewMeetupWindow.js
//CreateNewMeetUpView Component Constructor
function CreateNewMeetupWindow() {
var self = Ti.UI.createWindow({
layout : 'vertical',
backgroundColor:'white'
});
var contactsField = Ti.UI.createTextField({
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color : '#336699',
width : 400,
height : 60
});
self.add(contactsField);
var locationField = Ti.UI.createTextField({
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color : '#336699',
width : 400,
height : 60
});
self.add(locationField);
var lblNotifyMe = Ti.UI.createLabel({
color : 'black',
text : 'Notify me when s/he is',
textAlign : Ti.UI.TEXT_ALIGNMENT_LEFT,
width : 'auto',
height : 'auto'
});
self.add(lblNotifyMe);
var btnGo = Ti.UI.createButton({
title : 'Go',
height : 'auto',
width : 100
});
btnGo.addEventListener('click', function() {
// Check console
Ti.API.info('User clicked the button ');
});
self.add(btnGo);
return self;
};
2) after installing the app onto my device, i tried to launch it. the app shows momentarily (about 3 seconds maybe) and then it shuts down by itself. i guess its an app crash? but it works well on the emulator.
Titanium experts please help :$
You can use the following methods in your program to navigate between windows
Method 1
//Your app.js file
var button = Ti.UI.createButton({
height:44,
width:'auto',
title:'Create New Meetup',
top:20
});
self.add(button);
button.addEventListener('click', function() {
//By doing this you're opening a new window
var newWindow = Ti.UI.createWindow({
url : "ui/common/CreateNewMeetupWindow.js",//Provide the correct path here
fullscreen: false
});
newWindow.open();
});
//Your CreateNewMeetupWindow.js file
var newWindow = Ti.UI.currentWindow;
var contactsField = Ti.UI.createTextField({
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color : '#336699',
width : 400,
height : 60
});
newWindow.add(contactsField);
//You can add other controls here just like I added the contactsField
Method 2
var button = Ti.UI.createButton({
height:44,
width:'auto',
title:'Create New Meetup',
top:20
});
self.add(button);
button.addEventListener('click', function() {
var window = require('/ui/common/CreateNewMeetupWindow');
var newWindow = new window();
newWindow.open();
});
//Your CreateNewMeetupWindow.js file
function CreateNewMeetupWindow() {
var self = Ti.UI.createWindow({
layout : 'vertical',
backgroundColor:'white'
});
var contactsField = Ti.UI.createTextField({
borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color : '#336699',
width : 400,
height : 60
});
self.add(contactsField);
//Add other controls here
return self;
}
module.exports = CreateNewMeetupWindow;
You can use any single method from above. Do not mix the methods together.
You can use the following link for references
http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.Window-property-url
http://docs.appcelerator.com/titanium/latest/#!/api/Global-method-require
For your first problem, you are using the url property with a CommonJS object. So instead instantiate your window like this:
var newWindow = require("/ui/common/CreateNewMeetupWindow");
newWindow.open();
You would use the url property if your CreateNewMeetupWindow.js was not a CommonJS module.
Not sure what your second problem is, check your device logs and crash reports, otherwise there is no way to know what is going on.