2012-12-19 24 views
2

獲取數據我有一個RegistrationController屏幕存儲email-idpasswordDOBHeightWeightlogininController屏幕匹配email-idpassword到登錄的目的。如何從plist中的標籤

現在,在某些third屏幕我,如果我在字符串存儲在與LoginViewControlleremail-idpassword值從登錄的用戶,以顯示它的label.now的plist中只獲取HeightWeight並調用它在新的屏幕..如果它可以解決那麼如何從同一個的plist中獲取HeightWeight匹配,如果匹配,則給出了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

愛你的暱稱。 ^^ – Peres

回答

1

首先是店面這個NSArrayNSMutableArray後,從你的plist文件整體價值並以其objectAtIndexvalueForKey property..see整個波紋管例如獲得的價值..

UPDATE:

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

    } 

希望這可以幫到你...

+0

哪里哪里,我需要獲取第三數據 –

+0

使用此代碼中的LoginController或thirdview你想獲取數據並顯示它的兄弟.. –

+0

好吧,因爲你說的存儲在NSMutableArray但如何plist知道它,我登錄的用戶從相同的S ....因爲有多個註冊用戶中的plist –

1

當你輸入登錄屏幕上的憑據來檢查時,它匹配所取的plist的證書,那麼傳遞的plist下一個控制器。做這樣的事情

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

在UserViewController你將有一個NSDictionary的實例來存儲數據顯示,希望這將有助於你

+0

沒有將我推到UserViewController ......我不想推....我想,當我在UserViewController然後我要檢查登錄用戶,並獲取特定用戶的身高和體重 –

+0

你的問題是你在LoginViewController你需要再搭配3屏幕上的憑證,你需要顯示looged用戶的HIGHT和widht吧,如果你是管理單獨的plist爲每一位用戶,然後你爲什麼不保存的plist匹配用戶名和密碼後,和發送的plist至3視圖控制器其中包含的用戶 – Talha

+0

thts笏我did..thanks所有數據......得到的結果 –