2014-08-27 18 views
0

當文本字段被編輯時,我在NSDictionary中將textfield對象設置爲「True」。 然後for循環檢查已編輯的文本字段。使用Parse將編輯後的文本字段更新爲數據庫。我想這樣我的for-loop知道哪些文本字段應該更新我的數據庫中的哪一列。 for循環應該檢查它是否應該更新爲int或string。這可能嗎?for-loops將正確的文本字段更新到數據庫中的正確列(解析)

我的代碼:

-(IBAction)btnSaveRecipe:(id)sender{ 
for (NSNumber *key in allEditedTextfields) { //Loop through all keys 

    if ([[allEditedTextfields objectForKey:key] boolValue] == TRUE) { //See if it was edited 

     UITextField *tField = (UITextField *)[self.view viewWithTag:[key intValue]]; 
     PFQuery *query = [PFQuery queryWithClassName:@"Recipe"]; 
     [query getObjectInBackgroundWithId:detailId block:^(PFObject *recipe, NSError *error){ 

     if (tField.tag == 110) { 
      recipe[@"recipeName"] = recipeName.text; 
     } 
     if (tField.tag == 111){ 
      recipe[@"recipePrepTime"] = recipePrepTime.text; 
     } 
     if (tField.tag ==112){ 
      recipe[@"recipeRating"] = recipeRating.text; //recipeRating is an integer 
     } 
     }]; 

    } 
} 

[allEditedTextfields removeAllObjects]; 

}

+0

您可以將tField.tag作爲參數傳遞,然後您的循環將準確知道要更新的內容。 – Marius 2014-08-27 09:18:39

+0

你是什麼意思?你有一個例子嗎? – Dridia 2014-08-27 09:21:46

回答

0

首先關於recipe[@"recipeRating"]確保你CONVER recipeRating.text詮釋一個NSNumber否則不能保存解析。

接下來關於解析更新正確的列,解析使它非常容易。只需撥打[recipe saveInBackground]即可解析並自動檢測您已更改和更新的內容。您不必告訴它更新特定列

+0

所以你的意思是我應該刪除「if-statement」,並用「[recipe saveInBackground]」取代它? – Dridia 2014-08-27 12:48:59

+0

哦,不, if語句確實如此。只需在if語句的末尾添加'[recipe saveInBackground]'(在關閉之前) – 2014-08-27 16:10:15

+0

但是如果我有20個if語句,這會不會讓App本身佔用大量的內存或cpu-usage? – Dridia 2014-08-27 17:42:10