简体   繁体   English

iPhone / iPad重置后保存应用程序数据

[英]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. 下方链接中的照片说明了我当前正在使用的应用程序,名称是用户编辑自己的UITextfield,余额是标签,用户单击添加按钮添加所需的资金。 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. 然后,下面的两个文本字段可用于输入花费的货币及其使用方式,然后将其另存为UITable视图中的UITableviewCell。 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. 我目前遇到的问题是,当我重新启动iPhone设备时,以前输入的数据将恢复原状,这意味着用户将不得不重写所有无济于事的东西,因为这是一个用于监视储蓄的应用程序。 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. 我做了一些谷歌搜索,看看是否有针对此的修复程序,我遇到了NSUserDefaults并在我的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 有人可以提供采用其他方法的步骤,即使用户关闭其iPhone / iPad等后,也可以将输入的信息保留在原处。这是照片链接

http://imgur.com/DceJ7gs http://imgur.com/DceJ7gs

[[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 这是我用来添加资金的代码,这反过来会改变uilabel余额在上一张照片中显示的余额

- (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 thejobemuhammed@gmail.com 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];
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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