how can I pass the value in a textfield to a new tab/window?
I am trying to build a calculator type of application where you input some values on tab 1 and then when you press tab 2 the values from tab 1 are used and calculate results to displayed on tab 2. I can not find how to pass the values of the textfields to tab 2. How is this done?
Thanks in advance
6 Answers
-
You could always save the value of the textfield as a Property that is saved and available app wide.
Something like:
Titanium.App.Properties.setString("Textfield_tab1",textfield.value);Where textfield is the name of your textfield, and you'd call it by using
Titanium.App.Properties.getString("Textfield_tab1")
-
Took the basic example and added an event listener to update the fields on the second tab whenever it became active.
Like I said in the comment, there is no need for magic since it is all in one js file
// 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' }); win1.add(label1); var ageTf = Titanium.UI.createTextField({ color:'#336699', hintText:'field one', height:35, top:10, left:10, width:250, borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED }); win1.add(ageTf); var anotherTf = Titanium.UI.createTextField({ color:'#336699', hintText:'field two', height:35, top:50, left:10, width:250, borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED }); win1.add(anotherTf); // // create controls tab and root window // var win2 = Titanium.UI.createWindow({ title:'Tab 2', backgroundColor:'#fff' }); var tab2 = Titanium.UI.createTab({ icon:'KS_nav_ui.png', title:'Tab 2', window:win2 }); var label2 = Titanium.UI.createLabel({ color:'#999', text: '', font:{fontSize:20,fontFamily:'Helvetica Neue'}, textAlign:'center', width:'auto' }); win2.add(label2); // // add tabs // tabGroup.addTab(tab1); tabGroup.addTab(tab2); win2.addEventListener('focus', function(e) { Titanium.API.info ('in event listener ' + e); label2.text = anotherTf.value + ageTf.value; } ); // open tab group tabGroup.open();
-
Well I figured out an answer… the ones supplied did not work. I looked at the kitchen sink and the tab group listener using blur seemed to do the trick. here is my code and this works on Android and Iphone… but the index for Android is for the tab you are coming from and for the Iphone it is for the tab you clicked on. Not nice… If I could find a way to find the title of the actual tab that was click on… then we might have something.
// 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'
});win1.add(label1);
var ageTf = Titanium.UI.createTextField({
color:'#336699',
hintText:'field one',
height:35,
top:10,
left:10,
width:250,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win1.add(ageTf);
var anotherTf = Titanium.UI.createTextField({
color:'#336699',
hintText:'field two',
height:35,
top:50,
left:10,
width:250,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win1.add(anotherTf);//
// create controls tab and root window
//
var win2 = Titanium.UI.createWindow({
title:'Tab 2',
backgroundColor:'#fff'
});
var tab2 = Titanium.UI.createTab({
icon:'KS_nav_ui.png',
title:'Tab 2',
window:win2
});var label2 = Titanium.UI.createLabel({
color:'#999',
text: '',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:'auto'
});win2.add(label2);
//
// add tabs
//
tabGroup.addTab(tab1);
tabGroup.addTab(tab2);// blur event listener for tracking tab changes
tabGroup.addEventListener('blur', function(e)
{alert('tab blur - new index ' + e.index + ' old index ' + e.previousIndex); if(e.index == 0) { alert('tab 2 clicked'); label2.text = anotherTf.value + ageTf.value; }
});
// open tab group
tabGroup.open(); -
Well I figured out an answer… the ones supplied did not work. I looked at the kitchen sink and the tab group listener using blur seemed to do the trick. here is my code and this works on Android and Iphone… but the index for Android is for the tab you are coming from and for the Iphone it is for the tab you clicked on. Not nice… If I could find a way to find the title of the actual tab that was click on… then we might have something.
// 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'
});win1.add(label1);
var ageTf = Titanium.UI.createTextField({
color:'#336699',
hintText:'field one',
height:35,
top:10,
left:10,
width:250,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win1.add(ageTf);
var anotherTf = Titanium.UI.createTextField({
color:'#336699',
hintText:'field two',
height:35,
top:50,
left:10,
width:250,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win1.add(anotherTf);//
// create controls tab and root window
//
var win2 = Titanium.UI.createWindow({
title:'Tab 2',
backgroundColor:'#fff'
});
var tab2 = Titanium.UI.createTab({
icon:'KS_nav_ui.png',
title:'Tab 2',
window:win2
});var label2 = Titanium.UI.createLabel({
color:'#999',
text: '',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:'auto'
});win2.add(label2);
//
// add tabs
//
tabGroup.addTab(tab1);
tabGroup.addTab(tab2);// blur event listener for tracking tab changes
tabGroup.addEventListener('blur', function(e)
{alert('tab blur - new index ' + e.index + ' old index ' + e.previousIndex); if(e.index == 0) { alert('tab 2 clicked'); label2.text = anotherTf.value + ageTf.value; }
});
// open tab group
tabGroup.open(); -
Well I figured out an answer… the ones supplied did not work. I looked at the kitchen sink and the tab group listener using blur seemed to do the trick. here is my code and this works on Android and Iphone… but the index for Android is for the tab you are coming from and for the Iphone it is for the tab you clicked on. Not nice… If I could find a way to find the title of the actual tab that was click on… then we might have something.
// 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'
});win1.add(label1);
var ageTf = Titanium.UI.createTextField({
color:'#336699',
hintText:'field one',
height:35,
top:10,
left:10,
width:250,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win1.add(ageTf);
var anotherTf = Titanium.UI.createTextField({
color:'#336699',
hintText:'field two',
height:35,
top:50,
left:10,
width:250,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win1.add(anotherTf);//
// create controls tab and root window
//
var win2 = Titanium.UI.createWindow({
title:'Tab 2',
backgroundColor:'#fff'
});
var tab2 = Titanium.UI.createTab({
icon:'KS_nav_ui.png',
title:'Tab 2',
window:win2
});var label2 = Titanium.UI.createLabel({
color:'#999',
text: '',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:'auto'
});win2.add(label2);
//
// add tabs
//
tabGroup.addTab(tab1);
tabGroup.addTab(tab2);// blur event listener for tracking tab changes
tabGroup.addEventListener('blur', function(e)
{alert('tab blur - new index ' + e.index + ' old index ' + e.previousIndex); if(e.index == 0) { alert('tab 2 clicked'); label2.text = anotherTf.value + ageTf.value; }
});
// open tab group
tabGroup.open(); -
I have used following code to transfer a value from one view to another.
In the first view/window
Titanium.App.Properties.setString("rowValue",row);
var editWindow = Ti.UI.createWindow({url:'addNoteWindow.js'});
editWindow.open();
In the editWindow
var textValue = Titanium.App.Properties.getString("rowValue");