How can string for Settings-like page be shown after save?
I'm still unable to figure out how to reshow strings in a Settings-like page after the initial view. The code I have below displays correctly and appears to save the two strings, but once you go back and reshow the page I don't know how to display the saved strings rather than the hintText.
//if (Titanium.App.Properties.hasProperty("twitterusername") == true){
//Titanium.App.Properties.getString("twitterusername");
//});
//if (Titanium.App.Properties.hasProperty("twitterpassword") == true){
//Titanium.App.Properties.getString("twitterpassword");
//});
var row1 = Ti.UI.createTableViewRow();
var row2 = Ti.UI.createTableViewRow();
var label1 = Ti.UI.createLabel({
text:'Username:',
fontSize:15,
fontWeight:'bold',
height:30,
width:90,
left:10
});
var label2 = Ti.UI.createLabel({
text:'Password:',
fontSize:15,
fontWeight:'bold',
height:30,
width:90,
left:10
});
var tf1 = Titanium.UI.createTextField({
keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
autocorrect:false,
//text:twitterusername,
hintText:'Your Username',
left:100,
width:180,
clearButtonMode:Titanium.UI.INPUT_BUTTONMODE_ONFOCUS,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_NONE
});
var tf2 = Titanium.UI.createTextField({
keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
autocorrect:false,
passwordMask:true,
//text:twitterpassword,
hintText:'Your Password',
left:100,
width:180,
clearButtonMode:Titanium.UI.INPUT_BUTTONMODE_ONFOCUS,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_NONE
});
row1.add(label1);
row1.add(tf1);
row2.add(label2);
row2.add(tf2);
var data = [];
data[0] = Ti.UI.createTableViewSection();
data[0].add(row1);
data[0].add(row2);
var tv = Titanium.UI.createTableView({
style: Titanium.UI.iPhone.TableViewStyle.GROUPED,
data:data,
rowHeight:50
});
Titanium.UI.currentWindow.add(tv);
tf1.addEventListener('return',function(e){
Titanium.App.Properties.setString("twitterusername",tf1.value);
Titanium.App.Properties.setString("twitterpassword",tf2.value);
tf2.focus();
});
tf2.addEventListener('return',function(e){
Titanium.App.Properties.setString("twitterusername",tf1.value);
Titanium.App.Properties.setString("twitterpassword",tf2.value);
});
tf1.addEventListener('blur',function(e){
Titanium.App.Properties.setString("twitterusername",tf1.value);
Titanium.App.Properties.setString("twitterpassword",tf2.value);
});
tf2.addEventListener('blur',function(e){
Titanium.App.Properties.setString("twitterusername",tf1.value);
Titanium.App.Properties.setString("twitterpassword",tf2.value);
5 Answers
-
Your statement should be something like this:
if (Titanium.App.Properties.getString("twitterusername") != null){ tf1.text = Titanium.App.Properties.getString("twitterusername"); }; else { tf1.hintText = "Your Username"; }
-
That sounds like it would work, but when I go back into the page the second time, it does not display the text or hintText
-
I'm thinking the problem is with my eventListeners to save the strings. Does anyone have a better way to do it without using a button (so it functions like the Settings app)?
-
Hi Troy, I just tested with the following code and it worked:
var win = Titanium.UI.currentWindow; var hasPropertyUsername = Titanium.App.Properties.hasProperty("twitterusername"); var hasPropertyPassword = Titanium.App.Properties.hasProperty("twitterpassword"); var labelUsername = 'Your Username'; var labelPassword = 'Your Password'; //Titanium.API.info('Has Property Username? >> ' + hasPropertyUsername); if (hasPropertyUsername != null){ var propertyUsername = Titanium.App.Properties.getString("twitterusername"); Titanium.API.info('Username Property Top: ' + propertyUsername); labelUsername = propertyUsername; }; if (hasPropertyPassword != null){ var propertyPassword = Titanium.App.Properties.getString("twitterpassword"); Titanium.API.info('Password Property Top: ' + propertyPassword); labelPassword = propertyPassword; }; var row1 = Ti.UI.createTableViewRow(); var row2 = Ti.UI.createTableViewRow(); var label1 = Ti.UI.createLabel({ text:'Username:', fontSize:15, fontWeight:'bold', height:30, width:90, left:10 }); var label2 = Ti.UI.createLabel({ text:'Password:', fontSize:15, fontWeight:'bold', height:30, width:90, left:10 }); var tf1 = Titanium.UI.createTextField({ keyboardType:Titanium.UI.KEYBOARD_DEFAULT, autocorrect:false, //text:labelUsername, hintText:labelUsername, left:100, width:180, clearButtonMode:Titanium.UI.INPUT_BUTTONMODE_ONFOCUS, borderStyle:Titanium.UI.INPUT_BORDERSTYLE_NONE }); var tf2 = Titanium.UI.createTextField({ keyboardType:Titanium.UI.KEYBOARD_DEFAULT, autocorrect:false, passwordMask:true, //text:labelPassword, hintText:labelPassword, left:100, width:180, clearButtonMode:Titanium.UI.INPUT_BUTTONMODE_ONFOCUS, borderStyle:Titanium.UI.INPUT_BORDERSTYLE_NONE }); row1.add(label1); row1.add(tf1); row2.add(label2); row2.add(tf2); var data = []; data[0] = Ti.UI.createTableViewSection(); data[0].add(row1); data[0].add(row2); var tv = Titanium.UI.createTableView({ style: Titanium.UI.iPhone.TableViewStyle.GROUPED, data:data, rowHeight:50 }); win.add(tv); tf1.addEventListener('return',function(e){ Titanium.App.Properties.setString("twitterusername",tf1.value); Titanium.App.Properties.setString("twitterpassword",tf2.value); tf2.focus(); Titanium.API.info('Entered the Retunr Listener'); Titanium.API.info('Username ' + e.value); Titanium.API.info('Username Property 2: ' + propertyUsername); }); tf2.addEventListener('return',function(e){ Titanium.App.Properties.setString("twitterusername",tf1.value); Titanium.App.Properties.setString("twitterpassword",tf2.value); }); tf1.addEventListener('blur',function(e){ Titanium.App.Properties.setString("twitterusername",tf1.value); Titanium.App.Properties.setString("twitterpassword",tf2.value); }); tf2.addEventListener('blur',function(e){ Titanium.App.Properties.setString("twitterusername",tf1.value); Titanium.App.Properties.setString("twitterpassword",tf2.value); });
-
That almost works, except the hintText for the password shows the user's password which doesn't work for me. I'm trying to figure out something similar now.