【问题标题】:Saving app data after iphone/ipad resetiphone/ipad重置后保存应用数据
【发布时间】:2015-01-02 05:55:57
【问题描述】:

下面链接中的照片说明了我目前正在开发的应用程序,名称是用户自己编辑的 UITextfield,余额是用户单击添加按钮以添加所需资金的标签。然后下面的两个文本字段允许输入花费的钱以及如何花费,然后将其保存为 UITable 视图中的 UITableviewCell。我现在遇到的问题是,当我重新启动我的 iPhone 设备时,之前输入的数据会被保留,这意味着用户将不得不重写所有无济于事的内容,因为这是一个监控储蓄的应用程序。我做了一些谷歌搜索,看看是否有解决这个问题的方法,我遇到了 NSUserDefaults,并在我的 viewDidLoad 中尝试了以下代码。

有人可以提供不同方法的步骤,即使在用户关闭他或她的 iPhone/iPad 等设备后,也可以将输入的信息保留在原处。这是照片链接

http://imgur.com/DceJ7gs

[[NSUserDefaults standardUserDefaults] synchronize];

这是我用来添加资金的代码,这反过来会改变上图中 uilabel Balance 显示的余额

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

【问题讨论】:

  • 您能发布您的代码吗?您是如何保存数据的。可能有问题
  • 这里没有尝试保存数据的代码。您是否阅读了 NSUserDefaults 类参考以了解如何使用它们?

标签: ios objective-c iphone


【解决方案1】:

将此代码添加到您要保存的位置。我不知道你在哪里保存。 使用此代码保存它

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

稍后使用此代码将其取回

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

【讨论】:

  • 告诉我你要保存哪个值
  • 您好,这对 UILabel 非常有用!谢谢你,但是我无法将 uitableview 或内容(uitableview 单元格)保存到 nsuserdefaults 中。
猜你喜欢
  • 1970-01-01
  • 2013-12-18
  • 2013-01-29
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多