2012-03-15 1 views
0

に基づいての.plistファイルに新しい鍵を書き込むと、テキストフィールドからに追加することができるの.plistに基づいて、配列、からpickerViewを移入することです。私はそれを働かせるが、それは1つのキーを追加し、新しいものが追加されたときにそれを置き換えます。たとえば、「hello」を追加して別のものを追加しようとすると、helloが何か他のものに置き換えられます。は、私が何をしたいテキストフィールド

- (IBAction)setContext:(id)sender { 

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 

NSString *path = [[NSBundle mainBundle] pathForResource:@"fullArray" ofType:@"plist"]; 
NSString *textFieldText = textField.text; 

NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 
[rootDict setValue:textField.text forKey:textField.text]; 

NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"fullArray.plist"]; 
[rootDict writeToFile:writablePath atomically: YES]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1 

NSString *plistPath = [documentsDirectory  stringByAppendingPathComponent:@"fullArray.plist"]; //3 

NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; 
NSArray *array = [dictionary allKeys]; 

arrayContext = [[NSMutableArray alloc] initWithArray:array]; 

[pickerView selectRow:0 inComponent:0 animated:YES]; 

[textField endEditing:YES]; 

[pickerView reloadAllComponents]; 

} 

どうすればこの問題を解決できますか?

答えて

2

OK]をクリックして、あなたがピッカーで何かを選ぶたびに新しいNSMutableDictionaryを作成している、正しいアプローチは次のとおりです。

  1. プロパティとしてNSMutableDictionaryを作成します。
  2. あなたが選ぶたびに、キーの新しい文字列を作成します。おそらく[NSString stringWithFormat:]を使用します。
  3. [mutableDictionary setObject:forKey]を使用して、新しい作成されたキーを使用して、変異体辞書の新しいオブジェクトを設定します。
+0

ここで私はコードhttp://stackoverflow.com/questions/9673140/create-plist-file-via-code/9673615#9673615 – LuisEspinoza

+0

で、同様の質問に答えると、あなたはveredictを持っていますか? – LuisEspinoza

+0

いいえ、私がしようとしているのは、textField.textを配列に追加して.plistに書き込むためのボタンがあるtextFieldを一番上​​に持つことです。 – user1222053

関連する問題