我有一个RegistrationController
屏幕可存储email-id
,password
,DOB
,Height
,Weight
和logininController
屏幕将email-id
和password
与登录目的相匹配。
现在,在某个third
屏幕中,我必须从登录用户的plist中仅提取Height
,Weight
以在标签上显示它。如果我存储来自email-id
字符串中的password
和LoginViewController
的值,并在新屏幕中调用它以匹配匹配项,然后提供Height
,Weight
.. if它会更正如何从同一个plist中获取Height
,Weight
。
如何从字符串中存储的plist中获取?
这是我的代码:
-(NSArray*)readFromPlist
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"XYZ.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];
NSArray *valueArray = [dict objectForKey:@"title"];
return valueArray;
}
- (void)authenticateCredentials {
NSMutableArray *plistArray = [NSMutableArray arrayWithArray:[self readFromPlist]];
for (int i = 0; i< [plistArray count]; i++)
{
id object = [plistArray objectAtIndex:i];
if ([object isKindOfClass:[NSDictionary class]]) {
NSDictionary *objDict = (NSDictionary *)object;
if ([[objDict objectForKey:@"pass"] isEqualToString:emailTextFeild.text] && [[objDict objectForKey:@"title"] isEqualToString:passwordTextFeild.text])
{
NSLog(@"Correct credentials");
return;
}
NSLog(@"INCorrect credentials");
} else {
NSLog(@"Error! Not a dictionary");
}
}
}
答案 0 :(得分:1)
当您在登录屏幕上输入凭据以检查其与提取的plist的凭据是否匹配时,然后将该plist传递给下一个控制器。做这样的事情
UserViewController *controller = [[UserViewController alloc] initWithNibName:@"UserViewController" bundel:nil];
[controller setUserDictionary:yourPlistDictionary];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
在UserViewController中你会有一个NSDictionary实例来存储要显示的数据,希望对你有所帮助
答案 1 :(得分:1)
首先从您的plist文件中获取整个值,然后将此NSArray
存储到NSMutableArray
并获取其objectAtIndex
和valueForKey
属性的值。请参阅下面的示例..
更新:
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"yourFileName" ofType:@"plist"];
NSArray *contentArray = [NSArray arrayWithContentsOfFile:plistPath];
NSMutableArray *yourArray = [[NSMutableArray alloc] initWithArray:contentArray];
for (int i = 0; i< [yourArray count]; i++)
{
id object = [yourArray objectAtIndex:i];
if ([object isKindOfClass:[NSDictionary class]]) {
NSDictionary *objDict = (NSDictionary *)object;
yourLableWeight.text = [[objDict objectForKey:@"Weight"];// set index with your requirement
yourLableHeight.text = [[objDict objectForKey:@"Height"];
}
希望这能帮到你......