2

我有一个RegistrationController屏幕来存储email-id, password, DOB,Height和屏幕来匹配Weight和 登录目的。logininControlleremail-idpassword

现在,在某些third屏幕中,我必须仅从 登录用户的 plist 中获取 , 以将其显示在标签上。如果我将值Height和in存储在字符串中并在新屏幕中调用它以匹配 if匹配然后给出, ..如果它更正,那么如何从同一 plist 中获取。Weightemail-idpasswordLoginViewControllerHeightWeightHeightWeight

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

2 回答 2

1

当您在登录屏幕上输入凭据以检查它何时将凭据与获取的 plist 匹配时,然后将该 plist 传递给下一个控制器。做这样的事情

UserViewController *controller = [[UserViewController alloc] initWithNibName:@"UserViewController" bundel:nil];
[controller setUserDictionary:yourPlistDictionary];
[self.navigationController pushViewController:controller animated:YES];
[controller release];

在 UserViewController 中,您将有一个 NSDictionary 实例来存储要显示的数据,希望对您有所帮助

于 2012-12-19T11:12:13.720 回答
1

首先从您的 plist 文件中获取整个值,然后将其存储NSArrayNSMutableArray并使用其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"];

    }

希望这可以帮助你...

于 2012-12-19T11:12:34.093 回答