当应用程序重新启动时,如何保留 UITextField 中存储在 plist 中的数据?

发布于 2024-08-29 08:21:44 字数 1518 浏览 1 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傻比既视感 2024-09-05 08:21:45

该错误是由于数组太小而触发的;您尝试在超出数组上限的索引处插入对象。

如果要同时保留索引和数据,请将索引存储在字典中:

NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value", string2, @"key", string1, @"index", [NSNumber numberWithInt:indexOfDataArray], nil];
[myArray addObject:myDict];

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:

NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value", string2, @"key", string1, @"index", [NSNumber numberWithInt:indexOfDataArray], nil];
[myArray addObject:myDict];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文