如何将TextField的内容保存到plist

时间:2013-02-06 05:00:57

标签: ios objective-c uitextfield ios-simulator plist

我想在plist中使用相应的Key-Values对保存TextFields的内容。 与密码字段一样,应使用密钥 - 密码和值 - (在textField中输入)保存。

我该怎么做?

并希望在其他课程中访问它。我可以做吗?如果是,那怎么办?

5 个答案:

答案 0 :(得分:1)

将资料添加到plist很容易。随后是完整的工作代码,将人员联系信息添加到plist中 -

// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];

// set the variables to the values in the text fields
self.personName = nameEntered.text;
self.phoneNumbers = [[NSMutableArray alloc] initWithCapacity:3];
[phoneNumbers addObject:homePhone.text];
[phoneNumbers addObject:workPhone.text];
[phoneNumbers addObject:cellPhone.text];

// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personName, phoneNumbers, nil] forKeys:[NSArray arrayWithObjects: @"Name", @"Phones", nil]];

NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

// check is plistData exists
if(plistData) 
{
    // write plistData to our Data.plist file
    [plistData writeToFile:plistPath atomically:YES];
}
else
{
    NSLog(@"Error in saveData: %@", error);
    [error release];
}

[source]

答案 1 :(得分:0)

您可以通过以下方式将textField的内容保存到Plist:

        NSMutableDictionary *_plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:pListPath];
        [_plistDict setValue:textField.text forKey:@"Password"];
        [_plistDict writeToFile:pListPath atomically:YES];

如果要从pList检索该值,可以使用以下代码:

NSMutableDictionary *_plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:pListPath];
    NSString *status = [NSString stringWithFormat:@"%@",[_plistDict objectForKey:@"Password"]];

答案 2 :(得分:0)

是的,在词典的键中添加NSMutableDictionary UITextFeild属性text的值。像

         NSMutableDicitonary * dictionary = [[NSMutableDictionary alloc] init];
        [dictionary setObject:textfield.text forKey:@"username"];
        [dictionary setObject:textfield.text forKey:@"password"];

将词典保存在plist中并在需要的地方使用它。

答案 3 :(得分:0)

 NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString *aFilePath = [NSString stringWithFormat:@"%@/plistName.plist", aDocumentsDirectory];

NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:aFilePath];


NSMutableDictionary *newComment = [NSMutableDictionary dictionary];
[newComment setValue:userName.text forKey:@"username"];
[newComment setValue:password.text forKey:@"password"];


[plistArray addObject:newComment];
[plistArray writeToFile:filePath atomically:YES];

答案 4 :(得分:0)

最佳做法会在NSUserDefaults而不是plist中保存这些信息。可以从项目的任何位置访问NSUserDefautls。

保存:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[yourTextfield text] forKey:@"password"];

检索:

 NSString *myPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];