下面链接中的照片说明了我正在处理的应用程序,名称是用户编辑自己的UITextfield,余额是标签,用户点击添加按钮添加所需资金。然后下面的两个文本字段允许输入花费和花费的方式,然后将其保存为UITable视图中的UITableviewCell。我现在遇到的问题是,当我重新启动我的iPhone设备时,之前输入的数据已经休息,这意味着用户将不得不重写所有无效的内容,因为这是一个监控节省的应用程序。我做了一些谷歌搜索,看看是否有一个修复,我遇到了NSUserDefaults并在我的viewDidLoad中尝试了以下代码。
有人可以提供不同方法的步骤,即使在用户关闭他/她的iPhone / iPad等之后,也可以将输入的信息保持在原地。继承人的照片链接
[[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];
}
答案 0 :(得分:1)
将此代码添加到要保存的位置。我没有得到你在哪里储蓄。 使用此代码保存它
NSString *valueToSave = @"hereis yourvalue";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferencekyename"];
[[NSUserDefaults standardUserDefaults] synchronize];
稍后使用此代码将其取回
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"preferencekyename"];