简体   繁体   中英

How to add NSUserDefault data to a UITextField?

I have 3 UIVIewControllers that have NSUsuerDefaults to store data. My 4th UIViewController is used to display the total data collected between the first 3 UIViewControllers. How do I collect the data and display it in a UITextfield? Here is an example of my NSUserDefault code that I'm using: [tex setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"storedTextValue1"]]; [tex setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"storedTextValue22"]];
[tex setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"storedTextValue43"]]; I want to add this collected data to a UITextfield on my 4th UIViewController.

Try this:

textField.text = [[NSUserDefaults standardUserDefaults]  objectForKey:@"storedTextValue1"];

Or

NSString *storedValue1 = [[NSUserDefaults standardUserDefaults]  objectForKey:@"storedTextValue1"];
textField.text = storedValue1;

I think you should use

tex setText:[[NSUserDefaults standardUserDefaults] valueForKey:@"storedTextValue43"];

So basically you need to use valueForKey: in place of objectForKey:

Hope this helps you.

EDIT :

[text setText:[NSString stringWithFormat:@"%@ %@ %@",[[NSUserDefaults standardUserDefaults] valueForKey:@"storedTextValue43"],[[NSUserDefaults standardUserDefaults] valueForKey:@"storedTextValue1"],[[NSUserDefaults standardUserDefaults] valueForKey:@"storedTextValue22"]]];

EDIT-2:

UITextField(Total) is Say txtTotal

UITextField(storetext1) is say txtStore1

UITextField(storetext2) is say txtStore2

UITextField(storetext3) is say txtStore3

Now simply do this as

int sum = [txtStore1.text intValue] + [txtStore2.text intValue] + [txtStore3.text intValue]
txtTotal.text = [NSString stringWithFormat:@%d",sum];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM