passing a variable declared in one class to another class - objective-c

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

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

Save value of the variable

Here is the NSUserdefaults code.
-(IBAction)SaveButton:(id)sender {
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSString* Assignment0Text = screen0.text;
[defaults setObject:Assignment0Text forKey:#"Assignment0Text"];
[defaults synchronize];
}
-(IBAction)LoadButton:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString* temp0 = [defaults objectForKey:#"Assignment0Text"];
screen0.text = temp0;
}
This code above means that the program only saves and loads the string value inside the textfield/label.
My question is, how can I modify this code so that the NSUserDefaults can save and load the value of a variable (in this case int & float), not the text inside the textfield/label? So that when I load the program, all the integer/float values are the same. Not what's written in the text inside the textfield/label are the same. All that the screen is showing is a float number with variable called runningtotal.
screen0.text = [NSString stringWithFormat:#"%2.2f", runningtotal];
if I understand your question correctly, you should use wrappers for variables like int:
int value = 5;
[defaults setObject:#(value) forKey:#"Assignment0Number"];
the magic is the NSUserDefaults can not store non-object values like 5. You need to create NSObject object, store the value within it, and then save this object into the NSUserdefaults. That is what happens when I write '#(value)'.
If to be more precise, #(value) is literal for [NSNumber numberWithInt:value], and since NSNumber is the object and subclass of NSObject, the value can be stored now.
EDIT:
here is what you probably want (I assume that runningtotal is the class variable):
-(IBAction)SaveButton:(id)sender {
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:#(runningtotal) forKey:#"Assignment0Text"];
[defaults synchronize];
}
-(IBAction)LoadButton:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber* temp0 = [defaults objectForKey:#"Assignment0Text"];
runningtotal = [temp0 intValue];
}

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
......
}`

Setting value in nsuserdefaults

I'm new to Objective-C and XCode and I've encountered such a problem.
How do you properly save the value to NSUserDefaults.
I have value in
[_textFields objectAtIndex:2]).text
And trying to do this
[[[NSUserDefaults standardUserDefaults] setValue:((UITextField *)[_textFields objectAtIndex:2]).text)) forKey:#"Phone"];
[[NSUserDefaults standardUserDefaults] synchronize];
How do you do it properly?
Use setObject:forKey: instead of setValue:forKey:
[[[NSUserDefaults standardUserDefaults] setObject:((UITextField *)[_textFields objectAtIndex:2]).text)) forKey:#"Phone"];
Update:
You code will work if you really have that text field in the array. Here is the step by step broken down code to make it more clear:
UITextField* textField = [_textFields objectAtIndex:2]; // get your text field
NSString* text = textField.text; // get the actual value we are going to store
[[[NSUserDefaults standardUserDefaults] setObject:text forKey:#"Phone"]; // store it
You are working with strings values. You should use setObjectForKey (save) and objectForKey (read). Here's an example
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *yourText = [[_textFields objectAtIndex:2] text]; // Your text
// Save
[userDefaults setObject:yourText forKey:kKey];
[userDefaults synchronize];
// Read
NSString *result = (NSString *) [userDefaults objectForKey:kKey];
Use method setObject:forKey: instead of setValue:forKey:
In NSUserDefaults class:
[[NSUserDefaults standardUserDefaults] setObject:"Your Object" forKey:#"Phone"];
You need to use the setObject:forKey: if you want to store property list objects (dictionaries, arrays, strings and numbers) in NSUserDefaults. See the docs.

Saving an integer value for later on an iPhone

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/