如何从Label中的pList获取数据

时间:2012-12-19 11:01:38

标签: iphone ios ios5 plist

我有一个RegistrationController屏幕可存储email-idpasswordDOBHeightWeightlogininController屏幕将email-idpassword与登录目的相匹配。

现在,在某个third屏幕中,我必须从登录用户的plist中仅提取HeightWeight以在标签上显示它。如果我存储来自email-id字符串中的passwordLoginViewController的值,并在新屏幕中调用它以匹配匹配项,然后提供HeightWeight .. if它会更正如何从同一个plist中获取HeightWeight

如何从字符串中存储的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");
        }
    }
}

2 个答案:

答案 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并获取其objectAtIndexvalueForKey属性的值。请参阅下面的示例..

更新:

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

    }

希望这能帮到你......