当应用程序重新启动时,如何保留 UITextField 中存储在 plist 中的数据?
我使用 plist 来存储 UITextFields 中输入的数据。但是,当我重新启动应用程序时,之前输入的所有数据都被删除。如何保留 Plist 中的数据。我有一个 UITableView,当触摸单元格时,会出现一个带有两个 UITextField 的视图。名称字段和描述字段。我就是这样存储数据的。
我的代码是。
-(void)save:(id)sender
{
indexOfDataArray = temp;
NSString *string1 = [[NSString alloc]init];
NSString *string2 = [[NSString alloc]init];
string1 = nameField.text;
string2 = descriptionField.text;
NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:string2, string1, nil];
//NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value", string2, @"key", string1, @"index", [NSNumber numberWithInt:indexOfDataArray], nil];
[myArray addObject:myDict];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"tableVideoData.plist"];
[myArray writeToFile:path atomically:YES];
UIAlertView *alertMesage = [[UIAlertView alloc] initWithTitle: @"Save Alert" message:@"The data entered is saved" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil] ;
[alertMesage show];
[alertMesage release];
}
文件的路径是
/Users/srikanth/Library/Application Support/iPhone Simulator/User/Applications/31DEFEE7-A468-44BD-B044-53BEA3391C1A/Documents
但问题是每次我重新启动应用程序时,数据都会在新文件夹中的新 plist 文件中创建。那么,如何将数据存储在一个 plist 中呢?
谢谢。
I am using the plist to store the data entered in the UITextFields. But, when I restart my application all the data entered previously was deleted. How can I retain the data in the Plist. I have a UITableView and when a cell is touched a view appears with two UITextFields. nameField and descriptionField. I stored the data in this way.
My code is.
-(void)save:(id)sender
{
indexOfDataArray = temp;
NSString *string1 = [[NSString alloc]init];
NSString *string2 = [[NSString alloc]init];
string1 = nameField.text;
string2 = descriptionField.text;
NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:string2, string1, nil];
//NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value", string2, @"key", string1, @"index", [NSNumber numberWithInt:indexOfDataArray], nil];
[myArray addObject:myDict];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"tableVideoData.plist"];
[myArray writeToFile:path atomically:YES];
UIAlertView *alertMesage = [[UIAlertView alloc] initWithTitle: @"Save Alert" message:@"The data entered is saved" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil] ;
[alertMesage show];
[alertMesage release];
}
The path of the file is
/Users/srikanth/Library/Application Support/iPhone Simulator/User/Applications/31DEFEE7-A468-44BD-B044-53BEA3391C1A/Documents
But the problem is every time I restart the application the data is created in new plist file in new folder. So, how can I store the data in one plist ?
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误是由于数组太小而触发的;您尝试在超出数组上限的索引处插入对象。
如果要同时保留索引和数据,请将索引存储在字典中:
The error is triggered by the fact that the array is too small; you try to insert an object at an index that is beyond the upper bound of the array.
If you want to keep both the index and the data, store the index in the dictionary: