在熟悉核心数据时,我发现自己对尝试添加数据时传递各种视图控制器 (VC) 的问题感到困惑。
例如,在苹果提供的 CoreDataRecipes 项目作为示例 ( http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html ) 他们使用以下方法
当用户想要将配方添加到主表视图中显示的配方列表中并点击添加按钮时,主表视图控制器(称为 RecipeListTableViewController)会创建一个新的托管对象(配方),如下所示:
- (void)add:(id)sender {
// To add a new recipe, create a RecipeAddViewController. Present it as a modal view so that the user's focus is on the task of adding the recipe; wrap the controller in a navigation controller to provide a navigation bar for the Done and Save buttons (added by the RecipeAddViewController in its viewDidLoad method).
RecipeAddViewController *addController = [[RecipeAddViewController alloc] initWithNibName:@"RecipeAddView" bundle:nil];
addController.delegate = self;
Recipe *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:self.managedObjectContext];
addController.recipe = newRecipe;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[addController release];
}
这个新创建的对象(一个Recipe)被传递给RecipeAddViewController。RecipeAddViewController 有两个方法,保存和取消,如下:
- (void)save {
recipe.name = nameTextField.text;
NSError *error = nil;
if (![recipe.managedObjectContext save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self.delegate recipeAddViewController:self didAddRecipe:recipe];
}
- (void)cancel {
[recipe.managedObjectContext deleteObject:recipe];
NSError *error = nil;
if (![recipe.managedObjectContext save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self.delegate recipeAddViewController:self didAddRecipe:nil];
}
我对这种设计方法感到困惑。为什么RecipeListViewController 应该在我们知道用户是否真正想要输入新的食谱名称并保存新对象之前创建对象?为什么不将 managedObjectContext 传递给 addRecipeController,并等到用户点击保存来创建对象并用数据填充其字段?如果毕竟没有要添加的新配方,这可以避免必须删除新对象。或者为什么不在RecipeListViewController 和RecipeAddController 之间来回传递食谱名称(字符串)?
我问是因为我很难理解何时在 segues 之间传递字符串、何时传递对象以及何时传递 managedObjectContexts ......
非常感谢任何指导,包括。任何指向讨论的设计理念的链接。