开发者

storing input into variable

开发者 https://www.devze.com 2022-12-14 03:24 出处:网络
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 var

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. Howeve开发者_开发知识库r, 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!


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];


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"];

0

精彩评论

暂无评论...
验证码 换一张
取 消