Saving an integer value for later on an iPhone - objective-c

How do you save a value from a text feild so that can be saved and displayed later, on a differnt opening.

Use NSUserDefaults:
NSUserDefaults prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:10 forKey:#"high_score"];
[prefs synchronize];

In a file ?
http://www.csharphelp.com/2005/12/simple-text-file-operations-in-c/

Related

How do I get the text written in UITextView/UITextField automatically saved?

I have created about 10-20 UITextViews, but everytime I write something in the simulator, it disappears when exiting the viewcontroller, going back to the "menu" and then clicking back to the viewcontroller with the UITextViews. I really need the text to be saved, but I can't figure out how to do it, despite looking on Youtube etc.
You can have separate save and load method for each uitextview or you can store them in json file. Input : Textfield.text, Section : which textfield do you want to save and load. Then you just call the load method in the ViewDidLoad.
- (void) saveTemporary:(NSString *)input :(NSString *)section
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:input forKey:[NSString stringWithFormat:#"textfield_%#",section]];
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(NSString *)loadTemporary :(NSString *)section
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadingString = [defaults objectForKey:[NSString stringWithFormat:#"textfield_%#",section]];
return loadingString;
}
Here is for the json Tutorial :
http://www.raywenderlich.com/5492/working-with-json-in-ios-5

NSUserDefaults Reset First Run

I have an app already in the AppStore that uses NSUserDefaults. Some of the defaults are Default settings that I go ahead and set when the app is first launched, and then the user is allowed to change them later if they wish. So, in my AppDelegate appDidFinishLaunchingWithOptions I put:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:#"notFirstRun"]) {
[defaults setBool:YES forKey:#"notFirstRun"];
[defaults setInteger:0 forKey:#"verseKey"];
[defaults synchronize];
}
The issue I am having now is I want to add some more Default settings in the NSUserDefault category, so I want to make it look like this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:#"notFirstRun"]) {
[defaults setBool:YES forKey:#"notFirstRun"];
NSString *smalltitle = #"4";
NSString *smallarticle = #"3";
[defaults setObject:smalltitle forKey:#"Title"];
[defaults setObject:smallarticle forKey:#"Article"];
[defaults setInteger:0 forKey:#"verseKey"];
[defaults synchronize];
}
I know that this will cause an issue for those who have already downloaded the app, and are merely updating it. They will not run that code because the notFirstRun Bool has already been set to YES. Any thoughts on what I should do here?
The proper solution is to not actually populate NSUserDefaults with default values. Instead, use the registerDefaults: method.
At app startup you do:
NSUserDefaults *default = [NSUserDefaults standardUserDefaults];
[defaults registerDefaults:#{
#"Title" : #"4",
#"Article" : #"3",
#"verseKey" : #0
}];
That's it. Call this every time the app is run. These defaults are not actually persisted. The value is only returned if there isn't already an explicit value for the key. You can update these defaults all you want without affecting any existing values.
Make a new notFirstRun Boolean value (i.e. notFirstRunTwo). That will be 'NO' for existing users, too.
I suggest to do the following :
check userdefaults for stored app version if it is equal to the current or not , if not.
store the current app version and do your first launch initialization
the code
`
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *appVersion = [NSString stringWithFormat:#"%#",[[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleShortVersionString"]];
if (! [defaults objectForKey:appVersion ])
{
/// store the current version and then do your first run functions
[defaults setObject:[NSNumber numberWithInt:1] forKey:appVersion];
/// here do your first run
......
}`

passing a variable declared in one class to another class

I was trying to set a number in a textfield in one view, that is controlled by one class and make this number appear in a label that is in another view controlled by other class, how do i do it??
Very simple way is NSUserDefault. I don't recommend this but this a way to get data
Saving
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:#"TextToSave" forKey:#"keyToLookupString"];
// saving an NSInteger
[prefs setInteger:42 forKey:#"integerKey"];
// saving a Double
[prefs setDouble:3.1415 forKey:#"doubleKey"];
// saving a Float
[prefs setFloat:1.2345678 forKey:#"floatKey"];
// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];
Retrieving
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [prefs stringForKey:#"keyToLookupString"];
// getting an NSInteger
NSInteger myInt = [prefs integerForKey:#"integerKey"];
// getting an Float
float myFloat = [prefs floatForKey:#"floatKey"];
multiple repost like :
How can I pass a parameter into a view in iOS?
iphone Pass String to another .m file in project
you can also looks segue and protocol delegate

Saving hidden button state using NSUserDefaults Error

Trying to save button state as hidden when navigating off the view controller.
under IBAction
btnonce.hidden = YES;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:Act1Button.hidden forKey:#"isHidden"];
Calling it in ViewDidLoad
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
btnonce.hidden = [userDefaults valueForKey:#"isHidden"];
I'm getting the error: Implicit conversion of "BOOL" (aka signed char) to ID is disallowed with ARC
How to rectify this?
For saving the Bool value in NSUserDefaults use this code
[defaults setBool:Act1Button.hidden forKey:#"isHidden"];
For retrieving use this
btnonce.hidden = [userDefaults boolForKey:#"isHidden"];

NSUserDefaults will not load after leaving app

I set up multiple NSUserDefaults in my app that work for the most part. The NSUserDefaults are called from one ViewController and implemented at another (such as in a game with a level select). This seems to work well, until the user presses the back button on the level select screen. When they try to go back to the level select viewController, the NSUserDefaults are not loaded onto the screen.
Here is my code:
LevelSelectViewController:
- (void)viewDidLoad
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults synchronize];
[super viewDidLoad];
}
- (void)Level1DidFinish:(Level1 *)controller
{
[self dismissModalViewControllerAnimated:YES];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger rank = [userDefaults integerForKey:#"levelRank"];
{
if (rank == 1) {
button2.hidden = NO;
}
}
}
Level1ViewController:
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:1 forKey:#"levelRank"];
}
You need to call [userDefaults synchronize] after setting a value for a key. The automatic synchronization happens quite rarely, so in this case, while one completes a level, the sync won't have yet happened.
So modify your code like this:
Level1ViewController:
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:1 forKey:#"easyRank"];
[userDefaults synchronize];
}
Your first issue is you are creating an instance of userDefaults, but are not setting any values. So calling synchronize here is actually doing nothing:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults synchronize];
After you set a your Integer, then you need to call synchronize:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:1 forKey:#"easyRank"];
[userDefaults synchronize];
You're setting values for two different keys. What are you expecting to happen?
Either Level1ViewController needs to set a value for levelRank or LevelSelectViewController needs to read the value for the easyRank key.