gpt4 book ai didi

ios - iphone/ipad 重置后保存应用程序数据

转载 作者:行者123 更新时间:2023-11-29 02:24:21 29 4
gpt4 key购买 nike

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

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

http://imgur.com/DceJ7gs

[[NSUserDefaults standardUserDefaults] synchronize];

这是我用来添加资金的代码,它会改变上图中 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];
}

最佳答案

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

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

稍后使用此代码取回它

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

关于ios - iphone/ipad 重置后保存应用程序数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27737963/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com