MJJLAM
MJJLAM

Reputation: 183

Saving app data after iphone/ipad reset

The photo in the link below illustrates the app I am currently working on, the name is a UITextfield that the user edits him/herself and the balance is label in which the user clicks the add button to add their desired funds. Then the two textfields below that allow for the input of money spent and how it was spent, this is then saved as a UITableviewCell in the UITable view. The problem I am currently having right now is when I restart my iPhone device the previously inputted data is rested meaning the user will have to rewrite everything which wouldn't help as this is an app to monitor one's savings. I did some googling to see if there was a fix for this I came across NSUserDefaults and tried the following code in my viewDidLoad.

Could someone provide the steps for a different approach to keep the inputted information where it is even after the user turns off his or her iPhone/iPad etc. Heres the photo link

https://i.sstatic.net/Hupem.jpg

[[NSUserDefaults standardUserDefaults] synchronize];

This is the code I am using to add funds which in turn changes the balance displayed by the uilabel Balance in the above photo

- (IBAction)addFunds:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Add Funds" message:@"Please add your funds" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;

    [_inputStatement resignFirstResponder];
    [_spent resignFirstResponder];
    [myTableView resignFirstResponder];
    [_custName2 resignFirstResponder];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    //alertview input text was blank so we are doing a check
    if([[alertView textFieldAtIndex:0].text isEqual:@""] && buttonIndex == 1){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Input Required" message:@"Please fill in the textfield" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];

    } else {
        if(buttonIndex == 1){
        NSString *tempText = [alertView textFieldAtIndex:0].text;
        float toAdd = [tempText floatValue];
        float add = [_currentBalance.text floatValue];
        if(toAdd < 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You can't add a negative number, try again" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
        } else {
            add = add + toAdd;
            if(add >= 99999999.50){
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Well you broke it" message:@"You are not allowed to input this much. In the event you do have these funds, I apologize for putting in this restriction. Personally contact me at [email protected] for further inquiries." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
                [alert show];
            } else {

                _currentBalance.text = [NSString stringWithFormat:@"%.2f", add];

                if(!array){
                    array = [[NSMutableArray alloc]init];
                }

                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
                [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

                date1 = [NSDate dateWithTimeIntervalSinceNow:5];
                NSString *resultString = [dateFormatter stringFromDate:date1];

                [array insertObject:[NSString stringWithFormat:@"%@%@%@",@"$",tempText, @" deposited"] atIndex:0];

                [date insertObject:resultString atIndex:0];
                [details insertObject:@"This is a deposit" atIndex:0];

                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

                [self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            }
        }
    }
    [_inputStatement resignFirstResponder];
    [_spent resignFirstResponder];
    [myTableView resignFirstResponder];
}

Upvotes: 0

Views: 123

Answers (1)

Sport
Sport

Reputation: 8945

add this code where you want to save . i am not getting where are you saving . save it using this code

NSString *valueToSave = @"hereis yourvalue";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferencekyename"];
[[NSUserDefaults standardUserDefaults] synchronize];

to get it back later using this code

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
    stringForKey:@"preferencekyename"];

Upvotes: 1

Related Questions