textfield content in an iphone - iphone

As iam new the iphone development, i had created some textfields ,on click of save button it gets saved to a xml file ,and get disappears in the text field.On the next login, how to get the previously entered data in the same text field,so that i can modify the same data entered in the previous log in
thank you in advance

Read the xml file and set this to the textfield, textfield.text = #"your String";

To get previously entered data, you have to save them.
For this when user tap on save button you can save the value of each text field to NSUserDefaults for a key and when you want to get data you can get for that key.
To save-
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[textfield text] forKey:#"username"];
To get -
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *str = [defaults objectForKey:#"username"];

Related

Iphone App Registration User Data Log

At the start of my application i make my users to register first then take them to login View.I want to save the successfully registered users and let them get to the loginView instead of Register View.
How can i do this.As far i know iphone is registered as one user.Tell me what is the best way.And how to keep the user data and redirect to login view for a user?
Thanks
You can use NSUserDefaults to save user data such as login info.
Take a look at this tutorial and the reference
In Short, you would do something like:
Saving after registration:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:#"UserName" forKey:#"UserName"];
[prefs setObject:#"Pass" forKey:#"Pass"];
// This is suggested to synch prefs, but is not needed
[prefs synchronize];
Retrieving on app start:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *userName = [prefs stringForKey:#"UserName"];
NSString *pass = [prefs stringForKey:#"Pass"];

How to keep my data in a array-iphone sdk

I'm trying to create an application, and in that I'm receiving some contents from net and loading into and array, if I quit and run the app again i'm losing the data. How can I store the data in the app itself. Should I use some kind of database? If so which one and how can I implement that? I need to store only some string variables.
EDIT:
My app is tabbar based app, and have two tabs. Loading the arrays in one tab and loading that array in a table in the other VC. once I load the array and move to the second tab, the table is loaded with the array. If i go back and add few more values and then if i check, the added datas is not displayed in the table. And also, when I quit the app the table lose all the values, I need to keep the values in the table, even after i quit the app. How can I do that?
here is my code:
in viewdidload:
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
[simplePlistDb setBool:YES forKey:#"isItWorking"];
[simplePlistDb setObject:alertList forKey:#"myArray"];
[simplePlistDb synchronize];
in cellforrowatindexpath:-
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
BOOL flag = [simplePlistDb boolForKey:#"isItWorking"];
if (flag)
{
NSArray *myArray = [simplePlistDb objectForKey:#"myArray"];
for (NSString *str in myArray){
NSLog(#"Str:%#", str);
[loadArray addObject:str];
}
cell.textLabel.text = [loadArray objectAtIndex:indexPath.row];
}
Thank you.
You will want to look into Core Data if you want to save a decent amount of data to the iPhone. However, if not, you can just load the items in the array into a plist and load them from there at launch.
Plist Writing: How to create a new custom property list in iPhone Applications
CoreData: http://www.raywenderlich.com/934/core-data-tutorial-getting-started
Edit--->
As Nekto said, you can also use NSUserDefaults, but I advise mainly using NSUserDefaults for simple NSStrings and integers.
I think for your purposes NSUserDefaults will be enough : NSUserDefaults Class Reference.
Examples:
// save
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
[simplePlistDb setBool:YES forKey:#"isItWorking"];
[simplePlistDb setObject:[NSArray arrayWithObjects:#"very", #"cool", nil]];
[simplePlistDb synchronize];
// restore
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
BOOL flag = [simplePlistDb boolForKey:#"isItWorking"];
if (flag)
{
NSArray *myArray = [simplePlistDb objectForKey:#"myArray"];
for (NSString *str in myArray)
NSLog(#"%#", str);
}
You can use NSUserDefaults in order to store your data till the time your app is installed in your device.
How to write in NSuserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"yourObject1" forKey:#"correspopndingKey1"];
[defaults setObject:#"yourObject2" forKey:#"correspopndingKey2"];
[defaults synchronize];
How to read from NSuserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *temp1 = [defaults objectForKey:#"correspopndingKey1"];
NSString *temp2 = [defaults objectForKey:#"correspopndingKey2"];
You can store NSArray in a similar way.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:objArray forKey:#"correspopndingKey"];
[defaults synchronize];

NSUserDefaults problem

I have two views.In One view when i click on custom button it goes into second view,In that i have text view and that data must store in temperary memory so i used following code:
NSString *myString=txtvNote.text;
[txtvNote setText:#""];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:myString forKey:#"note"];
[standardUserDefaults synchronize];
}
and when i go back on 1st view by clicking on the add button it will save into database for that i used following code:
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standardUserDefaults)
val = [standardUserDefaults objectForKey:#"note"];
NSLog(#"Note::%#",val);
and then pass this value in insert query.
Now my problem is when i left that value blank,it takes the value which is inserted before.
sequence must be like this
//first set text to something
[txtvNote setText:#""];
//then use that text
NSString *myString=txtvNote.text;
myString is a pointer to the text in txtvNote. So if you set that text to #"" then myString will be empty as well. To preserve the value of txtvNote at the time of assignment use copy or reset the text after saving it:
NSString *myString=[[txtvNote.text copy] autorelease];
[txtvNote setText:#""];
....
That's the way NSUserDefaults works - every value is stored in your app's preferences, and like any other collection in Obj-C, it doesn't take nil values. Use instance of NSNull class or empty string. Or even better - use intermediate controller to store values and use key-value observing in your first view.

problem in saving a value in iphone

when i enter a value in the iphone simulator and press the save button it only keeps the value until the simulator is running. and when i restart the simulator and press the load button it shows an earlier value entered by me.i.e, it is not able to keep the new value and keeps only old value.
i am using following loops for saving a file and loading a file.
-(IBAction) save{
NSUserDefaults *sinner=[NSUserDefaults standardUserDefaults];
[sinner setObject:serverIP.text forKey:#"load"];
NSUserDefaults *king=[NSUserDefaults standardUserDefaults];
[king setObject:noc.text forKey:#"save"];
}
-(IBAction) load {
NSUserDefaults *sinner=[NSUserDefaults standardUserDefaults];
NSString *tempstring =[sinner stringForKey:#"load"];
serverIP.text = [NSString stringWithFormat:tempstring];
NSUserDefaults *king=[NSUserDefaults standardUserDefaults];
NSString *tempstring1 =[king stringForKey:#"save"];
noc.text = [NSString stringWithFormat:tempstring1];
}
// Your code
NSUserDefaults *king= [NSUserDefaults standardUserDefaults];
[king setObject:bookmarks forKey:#"Bookmarks"];
// saving it all
[king synchronize];
-(IBAction) save{
NSUserDefaults *sinner=[NSUserDefaults standardUserDefaults];
[sinner setObject:serverIP.text forKey:#"load"];
[sinner setObject:noc.text forKey:#"save"];
[sinner synchronize];
}
This should save the contents. You dont need two separate userdefaults. To load them you can try
-(IBAction) load {
NSUserDefaults *sinner=[NSUserDefaults standardUserDefaults];
NSString *tempstring =[sinner stringForKey:#"load"];
serverIP.text = tempstring;
NSString *tempstring1 =[sinner stringForKey:#"save"];
noc.text = tempstring1;
}
Hope this helps

iPhone Xcode Settings.bundle Plist Help

I followed the tutorial: http://useyourloaf.com/blog/2010/5/18/adding-a-settings-bundle-to-an-iphone-app.html
And the "Shuffle Switch" (that I just created based on the tutorial) was not in the Settings App. Every time I did an NSLog on the state of the switch, it would return "(null)". The "Slideshow Switch" (which was created the same way) worked fine.
My Settings bundle Root.plist file looks as follows: (copy link and paste into web browser)
i.imgur.com/kb8DT.png
Please help as I need to create, and access a Toggle Switch created in the .plist file. I am new to iPhone Programming.
Here's the code I'm using to set the user preference switch:
// Set the application defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:#"YES" forKey:#"ShuffleToggleKey"];
[defaults registerDefaults:appDefaults];
[defaults synchronize];
And here's the code I'm using to get the state of the user preference switch:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL enabled = [defaults boolForKey:#"ShuffleToggleKey"];
It seems, that you're putting string object and trying to get boolean value.
You should or get out the string like
NSString *enabledStr = [defaults stringForKey:#"ShuffleToggleKey"];
BOOL enabled = [enabledStr boolValue];
or put a boolean value in the first place like that:
[defaults setBool:YES forKey:#"ShuffleToggleKey"];
Then you can retrieve it as
BOOL enabled = [defaults boolForKey:#"ShuffleToggleKey"];