【发布时间】:2013-02-06 05:00:57
【问题描述】:
我想将 TextFields 的内容与相应的键值对保存在 plist 中。 Like Password 字段应与 Key-Password 和 Value-(在 textField 中输入)一起保存。
我该怎么做?
并希望在其他类中访问它。我可以做吗?如果是,那么如何?
【问题讨论】:
标签: ios objective-c uitextfield ios-simulator plist
我想将 TextFields 的内容与相应的键值对保存在 plist 中。 Like Password 字段应与 Key-Password 和 Value-(在 textField 中输入)一起保存。
我该怎么做?
并希望在其他类中访问它。我可以做吗?如果是,那么如何?
【问题讨论】:
标签: ios objective-c uitextfield ios-simulator plist
将东西添加到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];
}
【讨论】:
~/Library/Application Support/iPhone Simulator/User/Applications/ 此处的子文件夹中的某处 - mobiledevelopertips.com/xcode/…
您可以通过以下方式将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"]];
【讨论】:
是的,在 Dictionary 的键中使用 NSMutableDictionary 将 UITextFeild 属性的值添加到 text 中。喜欢
NSMutableDicitonary * dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:textfield.text forKey:@"username"];
[dictionary setObject:textfield.text forKey:@"password"];
将字典保存在 plist 中并在需要的地方使用它。
【讨论】:
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];
【讨论】:
最佳实践将这些信息保存在 NSUserDefaults 而不是 plist 中。可以从项目中的任何位置访问 NSUserDefautls。
保存:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[yourTextfield text] forKey:@"password"];
检索:
NSString *myPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
【讨论】:
[NSUserDefaults standardUserDefaults] 是单例的,因此可以从应用程序的任何位置访问它。你不必导入任何东西。无需在视图控制器之间传递对象。从一个地方保存并从另一个地方访问。
[yourTextfield text],这里yourTextfield应该是你声明的文本字段。在你的代码中改变它。