Hi I am seeking some insight into storing text input into a variable. My task is to take five text inputs, store the numbers the user inputs, then do some fancy math with them to create a solution variable to display. However, within the SDK I am getting confused quickly on how to actually store these correctly. I can't find much tutorials on this online, seeking gurus to help!
+1
A:
The question is a little vague… depending on what you want to do there are many ways:
- Temporary storage. Set up a NSString variable to hold the input, then do whatever you need to do to validate/convert etc
NSString *input1 = textField.text;
- Storage Over one run of the app. Setup a global variable in a custom class, model, or view controller that will not be dealloc'ed
[MyStorageClass setInput1:textField.text];
- Save over multiple launches of the app. Put the vars in NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:@"input1"];
coneybeare
2009-12-17 22:53:07
sorry. store the input into a variable that will be used only during run of app
HollerTrain
2009-12-17 22:54:15
+1
A:
Pretty straightforward stuff.
Declare the variable:
NSString *aString;
Then assign the string:
aString = someTextField.text;
If you're going to need the string to stick around for any period of time beyond the method where you capture it, you might consider making it a property of the class:
@property(nonatomic, copy) NSString *aString;
That's just for a string. If the user is inputting text that you want to treat as a number, you can do:
NSInteger userInputInteger = [someTextField.text intValue];
Danilo Campos
2009-12-17 22:53:45
TY Danilo. I really appreciate your help. I'm going to muck around with this and see what I can break :)
HollerTrain
2009-12-17 23:07:46
You got it! Good luck. The docs in Xcode are pretty thorough. Very useful once you learn to get around in them. A quick read through the Objective-C 2.0 Programming Language guide and you'll be all set.
Danilo Campos
2009-12-17 23:57:45