-3

this is a follow up from my question yesterday my .h is

@property (weak, nonatomic) IBOutlet UITextField *initialBudget;
@property (weak, nonatomic) IBOutlet UITextField *expenses;
@property (weak, nonatomic) IBOutlet UITextField *timeSpent;
@property (weak, nonatomic) IBOutlet UITextField *incomePerHour;

my .m is:

- (IBAction)calculateResults:(id)sender {
    double budget = [initialBudget.text doubleValue ];
    double expense = [expenses.text doubleValue];
    double time = [timeSpent.text doubleValue];

    double hourlyIncome = (budget - expense)/time;
   incomePerHour.text = [NSString stringWithFormat:@"%.2f", hourlyIncome];
}

Ive tried using different methods to keep the text saved in the ViewController, when I close the app the text disappears. Anyone have an idea? Ive only been studying Objective-C since the beginning of the summer so my apologies for the potential newbie questions.

Master Stroke
  • 5,108
  • 2
  • 26
  • 57
Jchou
  • 9
  • 1
  • 4
  • Have you connected the IBOutlet. – Anand Aug 14 '13 at 03:40
  • Which text do need to save ? and where you need to save ? – CodenameLambda1 Aug 14 '13 at 03:43
  • IBOutlets are connected I need to save all of the text – Jchou Aug 14 '13 at 03:45
  • 1
    Save it where? None of your code above does anything related to 'saving' anything. – davidf2281 Aug 14 '13 at 03:48
  • "save" is ambiguous. please describe what you see vs. what you expect to see. Is it that the text field incomePerHour does not contain a result after the method calculateResults runs? – danh Aug 14 '13 at 03:49
  • 1
    Have you done any searching on this subject? There are plenty of examples on using `NSUserDefaults` or other persistence mechanisms to save and restore data. – rmaddy Aug 14 '13 at 03:52
  • It contains a result. After I exit the Viewcontroller I would like to have the text from the result still in place. – Jchou Aug 14 '13 at 03:52
  • okay. there are many places to save. from variable names, this looks like a dictionary written to a file in NSCachesDirectory. Look at this SO, but consider NSCachesDirectory as the destination. http://stackoverflow.com/questions/1567134/how-can-i-get-a-writable-path-on-the-iphone – danh Aug 14 '13 at 03:52
  • 1
    For a small amount of data you can use NSUserDefaults - google it and you'll see it only needs a few lines of code. For a larger amount of data -- anything more than a handful of values -- you should look into Core Data. – davidf2281 Aug 14 '13 at 03:54
  • THank you, Im looking into NSUserdefaults now – Jchou Aug 14 '13 at 03:54

4 Answers4

3

Either save the text/string using NSUserDefaults or you can use SQLite to save/update/insert/retrieve the data.And when displaying it in textfield just retrieve it from sqlite…

Tutorials on sqlite..

1) http://www.techotopia.com/index.php/An_Example_SQLite_based_iPhone_Application

2) http://dblog.com.au/iphone-development-tutorials/iphone-sdk-tutorial-reading-data-from-a-sqlite-database/

EDIT :

You can use NSUserDefaults if you're storing small portions of data, while other approaches like coredata and sqlite are good to store bigger amounts of data.

Hope it helps you...

Master Stroke
  • 5,108
  • 2
  • 26
  • 57
  • Save as file is also applicable for OP's case, via `writeToPath` method – Raptor Aug 14 '13 at 04:21
  • NB, this answer isn't exactly correct: NSUserDefaults stores data permanently to disk, just like Core Data / SQLite. You can terminate the app as much as you like and the data will persist. – davidf2281 Aug 14 '13 at 06:44
  • @davidf2281...thanks for the info...yes and one more small correction..i will edit the answer.. – Master Stroke Aug 14 '13 at 06:47
1

It appears that your goal is to update the text of the incomePerHour text field with the calculated result of the other text fields.

You need to create a string from the result and update the text field's text property. You really should use an NSNumberFormatter setup for currency values.

double hourlyIncome = (budget - expense)/time;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *formattedString = [numberFormatter stringFromNumber:@(hourlyIncome)];
incomePerHour.text = formattedString;

Update - it appears that the actual question is about persisting the values between runs of the app. This answer is no longer all that appropriate.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Ive made the change, I need to know how to save all the outlets so they dont disappear when I close the app – Jchou Aug 14 '13 at 03:46
  • 2
    That's a completely different question. You need to update your question to make that goal clear. – rmaddy Aug 14 '13 at 03:46
0

You can use NSUserDefault to save your textField's result.

Try as following -

-(void)viewDidLoad
{
  //get and set value to your text field from the userdefault whenever you come from closeing the app..

  double result = [[NSUserDefault standardUserDefault]valueForKey:@"INCOME_VALUE"];
  incomePerHour.text = [NSString stringWithFormat:@"%.2f", result];
}

- (IBAction)calculateResults:(id)sender 
{
    double budget = [initialBudget.text doubleValue ];
    double expense = [expenses.text doubleValue];
    double time = [timeSpent.text doubleValue];

    double hourlyIncome = (budget - expense)/time;
   incomePerHour.text = [NSString stringWithFormat:@"%.2f", hourlyIncome];

   //set your result's value in the userdefault to access it later....
   [[NSUserDefault standardUserDefault]setDouble:hourlyIncome forKey:@"INCOME_VALUE"];
}

Hope this will help..

Rohan
  • 2,939
  • 5
  • 36
  • 65
  • 1
    You need to have a check for there being no value in `NSUserDefaults`. Otherwise this code will put `0.00` in the text field. – rmaddy Aug 14 '13 at 04:04
0

Yes, the easiest way to save your text is to use NSUSerDefault. Sqlite should be considered if you need structured data saved.

lenhhoxung
  • 2,530
  • 2
  • 30
  • 61