我正在关注这里的核心数据教程。我们有 RootViewcontroller 和 addRecipeViewController。
我列出了一些类和一些函数以及下面的流程屏幕,这样您就不会迷路
Recipe.h
#import <CoreData/CoreData.h>
@interface Recipes : NSManagedObject
{
}
@property (nonatomic, retain) NSString * recipeName;
@property (nonatomic, retain) NSString * cookingTime;
@end
addRecipeViewController.h
@class Recipes;
@interface AddRecipeViewController : UIViewController <UITextFieldDelegate> {
Recipes *recipes;
UITextField *textFieldOne;
UITextField *textFieldTwo;
}
addRecipeViewController.m
- (void)save {
1.recipes.recipeName = textFieldOne.text;
2.recipes.cookingTime = textFieldTwo.text;
3.NSError *error = nil;
4.if (![recipes.managedObjectContext save:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
[self dismissModalViewControllerAnimated:YES];
}
根视图控制器.m
- (void)insertNewObject {
AddRecipeViewController *addRecipeView = [[AddRecipeViewController alloc] initWithNibName:@"AddRecipeViewController" bundle:[NSBundle mainBundle]];
Recipes *recipes = (Recipes *)[NSEntityDescription insertNewObjectForEntityForName:@"Recipes" inManagedObjectContext:self.managedObjectContext];
addRecipeView.recipes = recipes;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: addRecipeView];
[self.navigationController presentModalViewController:navController animated:YES];
[addRecipeView release];
}
流量图片:
当Save
点击 addRecipeViewController 的事件时,它会保存recipes
到 managedObjectContext
. rootViewConroller 迟早会managedObjectContext
使用NSFetchedResultsController
问题:我不明白所有视图控制器的相同之处,以便在添加或删除后您manageObjectContext
将获得最新的更新manageObjectContext
rootViewController
Recipe
manageObjectContext
addRecipeViewController
请帮助我理解这个问题。
欢迎所有评论在这里。