如何从标签中的pList中获取数据
我有一个RegistrationController
屏幕来存储email-id
, password
, DOB
, Height
, Weight
和logininController
屏幕以匹配email-id
和password
login目的。
现在,在third
屏幕中,我只需要从login用户的plist中获取Height
,权Weight
,以便将其显示在label上。现在,如果将Login email-id
Controller中的email-id
和password
的值存储在string和call它在新的屏幕上如果匹配则给出Height
, Weight
如果它更正则从同一个plist取得Height
, Weight
。
我怎样才能从存储的plist中取出一个string?
这是我的代码:
-(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"); } } }
首先从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"]; }
希望这可以帮助你…
当您在login屏幕上input凭证以检查何时与获取的plist匹配时,将该plist传递给下一个控制器。 做这样的事情
UserViewController *controller = [[UserViewController alloc] initWithNibName:@"UserViewController" bundel:nil]; [controller setUserDictionary:yourPlistDictionary]; [self.navigationController pushViewController:controller animated:YES]; [controller release];
在UserViewController中,您将有一个NSDictionary实例来存储要显示的数据,希望对您有所帮助