0

我想用相应的键值对将 TextFields 的内容保存在 plist 中。Like Password 字段应与 Key-Password 和 Value-(在 textField 中输入)一起保存。

我怎样才能做到这一点?

并想在其他课程中访问它。我可以做吗?如果是,那么如何?

4

5 回答 5

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];
}

[来源]

于 2013-02-06T05:10:03.580 回答
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"]];
于 2013-02-06T05:06:51.337 回答
0

是的,在 Dictionary 的键中添加 in属性的NSMutableDictionary值。像UITextFeildtext

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

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

于 2013-02-06T05:07:46.720 回答
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];
于 2013-02-06T05:10:29.883 回答
0

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

保存:

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

检索:

 NSString *myPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
于 2013-02-06T05:11:29.360 回答