0
我想要做的是從基於.plist的數組中填充pickerView,該.plist可以從textField中添加。我有它的工作,但它只添加一個鍵,並在添加新鍵時替換它。例如,如果我添加「hello」,然後嘗試添加另一個,它會用別的替換hello。基於textField將新密鑰寫入.plist文件
- (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];
}
我該如何解決這個問題?
這裏我回答一個類似的問題,用代碼http://stackoverflow.com/questions/9673140/create-plist-file-via-code/9673615#9673615 – LuisEspinoza 2012-03-15 19:06:28
和,你有veredict? – LuisEspinoza 2012-03-15 21:35:00
不,我想要做的是在頂部有一個textField,並帶有一個按鈕將textField.text添加到數組中並將其寫入.plist。 – user1222053 2012-03-16 16:13:40