iphone/ipad 重置后保存应用程序数据
Saving app data after iphone/ipad reset
下面link中的照片说明了我目前正在开发的应用程序,名称是用户编辑的 UITextfield him/herself,余额是用户单击添加的标签按钮添加他们想要的资金。然后下面的两个文本字段允许输入花费的钱以及如何花费,然后将其保存为 UITable 视图中的 UITableviewCell。我目前遇到的问题是,当我重新启动 iPhone 设备时,之前输入的数据已恢复,这意味着用户将不得不重写所有内容,这无济于事,因为这是一个监控个人储蓄的应用程序。我做了一些谷歌搜索,看看是否有解决这个问题我遇到了 NSUserDefaults 并在我的 viewDidLoad 中尝试了以下代码。
有人可以提供不同方法的步骤,即使在用户关闭他或她的 iPhone/iPad 等之后,也能将输入的信息保留在原处。这是照片 link
[[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"];
下面link中的照片说明了我目前正在开发的应用程序,名称是用户编辑的 UITextfield him/herself,余额是用户单击添加的标签按钮添加他们想要的资金。然后下面的两个文本字段允许输入花费的钱以及如何花费,然后将其保存为 UITable 视图中的 UITableviewCell。我目前遇到的问题是,当我重新启动 iPhone 设备时,之前输入的数据已恢复,这意味着用户将不得不重写所有内容,这无济于事,因为这是一个监控个人储蓄的应用程序。我做了一些谷歌搜索,看看是否有解决这个问题我遇到了 NSUserDefaults 并在我的 viewDidLoad 中尝试了以下代码。
有人可以提供不同方法的步骤,即使在用户关闭他或她的 iPhone/iPad 等之后,也能将输入的信息保留在原处。这是照片 link
[[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"];