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

标签 iphone cocoa-touch uitableview uitextfield

我正在使用 plist 来存储在 UITextFields 中输入的数据。但是,当我重新启动应用程序时,之前输入的所有数据都被删除。如何保留 Plist 中的数据。我有一个 UITableView,当触摸单元格时,会出现一个带有两个 UITextField 的 View 。名称字段和描述字段。我就是这样存储数据的。

我的代码是。

-(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 中呢?

谢谢。

最佳答案

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

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

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

关于iphone - 当应用程序重新启动时,如何保留 UITextField 中存储在 plist 中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2683124/

相关文章:

iphone - 接到来电时,录制的视频已损坏,并且 AVFoundation 方法出现错误

iphone - 使 UITextView 对点击事件使用react。 (苹果手机)

ios - 切换到编辑模式时更改 UITableviewcell 中的 UILabel 宽度

ios - 在 iOS 中获取 UIGestureRecognizer 的 Action

iphone - 使用按钮将 View 与 Storyboard中的 TableView 连接起来

ios - 如何将 Storyboard UISearchBar 连接到以编程方式创建的 UISearchController?

加载 UIViewController 时的 iPhone UIActivityIndi​​catorView

iphone - 如何在 ios 中使用自定义形状创建 uiscrollview?

iphone - NSMutableDictionary 的排序

objective-c - 如何播放音频先完成音频文件然后再开始播放?